Factory Design Pattern Explained
The Factory pattern delegates the creation of objects to a dedicated method or class, instead of calling a constructor directly, so calling code doesn't need...
Published October 6, 2026
The Factory pattern moves object creation into a dedicated function or class so callers ask for what they need without knowing the concrete class. It makes adding new types easy without changing client code.
The concept
Instead of new Dog() or new Cat() scattered everywhere, you call createAnimal('dog'). The factory decides which class to instantiate.
Variants: simple factory (one function), factory method (subclasses decide) and abstract factory (families of related objects).
Callers talk to the factory
Practical examples
Simple factory
function createExporter(type) {
switch (type) {
case 'pdf': return new PdfExporter()
case 'csv': return new CsvExporter()
default: throw new Error('unknown ' + type)
}
}Registry-based, no switch
const registry = { pdf: PdfExporter, csv: CsvExporter }
const create = (t) => new registry[t]()Python
def make_notifier(kind):
return {'email': EmailNotifier, 'sms': SmsNotifier}[kind]()Common mistakes
- Factories with giant switch statements that grow forever.
- Using a factory when a plain constructor would do.
- Returning inconsistent types.
Key takeaways
- Centralise creation.
- Callers depend on an interface, not a class.
- Registries avoid switch sprawl.
FAQ
Factory vs Builder?
Factory picks which object; Builder assembles one complex object step by step.
Do I need classes?
No, plain functions work as factories.