creational
Factory Method
Define a creation method behind a common interface so specialized creators can decide which concrete product to instantiate without changing the surrounding workflow.
Problem
A workflow needs to create one of several related concrete objects, but constructing them directly in client code spreads creation logic and tightly couples the caller to specific implementations.
Integration notes
Factory Method is useful when framework hooks, integrations, or environment-specific workflows need to create interchangeable implementations behind a stable contract.
Tradeoffs
- Adds extra indirection and more types compared with direct construction
- Can feel unnecessarily abstract when object creation is simple and unlikely to vary
Languages
typescript, java, python, angular, react, react-native, csharp, dotnet
Platforms
Applications, APIs, Frameworks, Service layers
Variants
Scenarios
Document export
A reporting workflow uses a factory method to choose the right exporter for PDF, CSV, or other output formats while keeping the export flow consistent.
Language examples
Core languages
Frameworks and libraries
The creator defines the export workflow, while concrete exporters decide which document implementation gets instantiated.
interface DocumentFile {
export(): void;
}
class PdfDocument implements DocumentFile {
export(): void {
console.log("Exporting PDF document");
}
}
class CsvDocument implements DocumentFile {
export(): void {
console.log("Exporting CSV document");
}
}
abstract class DocumentExporter {
abstract createDocument(): DocumentFile;
runExport(): void {
const document = this.createDocument();
document.export();
}
}
class PdfExporter extends DocumentExporter {
createDocument(): DocumentFile {
return new PdfDocument();
}
}
class CsvExporter extends DocumentExporter {
createDocument(): DocumentFile {
return new CsvDocument();
}
}
const exporter: DocumentExporter = new PdfExporter();
exporter.runExport();Real-world examples
- Report and document exporters: Create different file exporters such as PDF, CSV, or spreadsheet generators behind one shared export workflow.
- Notification delivery providers: Instantiate email, SMS, or push sender implementations based on delivery channel or account configuration.
- Framework and integration extension points: Let subclasses or adapters choose which transport, provider, or product implementation a workflow should create.