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
Published at
2026-10-25 12:38:07 UTCEvent JSON
{
"id": "727001b1672580a9bebc15f051862b71628b6ee991d9cd8ef9d223ed2b5f54a3",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1792931887,
"kind": 1,
"tags": [
[
"t",
"tip"
]
],
"content": "Learn: Factory\n\nA factory creates objects for you, so the caller asks for \"a pdf exporter\" without naming the class.\n\n```csharp\nConsole.WriteLine(Create(\"pdf\").Export());\nstatic IExporter Create(string kind) =\u003e kind switch\n{\n \"pdf\" =\u003e new PdfExporter(),\n \"csv\" =\u003e new CsvExporter(),\n _ =\u003e throw new ArgumentException(kind),\n};\ninterface IExporter { string Export(); }\nclass PdfExporter : IExporter { public string Export() =\u003e \"PDF file\"; }\nclass CsvExporter : IExporter { public string Export() =\u003e \"CSV file\"; }\n```\n\nIt prints PDF file.\nUse it when: which class to create depends on input or configuration, or creating it is complicated.\n\nRead more: https://www.geeksforgeeks.org/system-design/factory-method-for-designing-pattern/\n\n#tip · TIP-052",
"sig": "3862270b3e36af5d1b68c71ed12fac953b1e6f3acdf0292ac4a28958edc7c48f89d834eefcd9f76f3847af1a7c11e676b2d56d65e874555645b677b7069fb596"
}