npub1u6…v0yg2 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-26 13:37:59 UTCEvent JSON
{
"id": "391ced7cc5e221fc1ebe88161d5a93ad64a0ded317c1d7035295114b196c17a6",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1793021879,
"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": "7b8cbe4f88654bf62c7549d45fb3292e5b1b9ac6ba13dc64518889b5e113ebfd54320b6c08470ec525a6383bf3058390ddd160ee874fb79b5ebe61522fbfe1df"
}