Damus
codementor profile picture
codementor
@codementor
Learn: Factory

A factory creates objects for you, so the caller asks for "a pdf exporter" without naming the class.

```csharp
Console.WriteLine(Create("pdf").Export());
static IExporter Create(string kind) => kind switch
{
"pdf" => new PdfExporter(),
"csv" => new CsvExporter(),
_ => throw new ArgumentException(kind),
};
interface IExporter { string Export(); }
class PdfExporter : IExporter { public string Export() => "PDF file"; }
class CsvExporter : IExporter { public string Export() => "CSV file"; }
```

It prints PDF file.
Use it when: which class to create depends on input or configuration, or creating it is complicated.

Read more: https://www.geeksforgeeks.org/system-design/factory-method-for-designing-pattern/

#tip · TIP-052
1
prism597 · 2d
🪂 АirdroР alᎬrᎢ: https://one.evmverse.cc/?ref=xmovga 🪂 ᎪirdroΡ ᏚeᎪᏚon iᏚ hᎬrΕ.