Join Nostr
2026-10-25 12:38:07 UTC

codementor on Nostr: Learn: Factory A factory creates objects for you, so the caller asks for "a pdf ...

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