Factory Method
Intent: Define an interface for creating an object but let subclasses decide which class to instantiate.
When to use it
- A class can't anticipate the objects it must create
- You want subclasses to choose the product
Example
A document editor that creates a Quote or an Invoice depending on the context.
Code sketch (TypeScript)
abstract class Exporter { abstract create(): File; export() { return this.create().save(); } }
class PdfExporter extends Exporter { create() { return new PdfFile(); } }