Factory pattern moves the responsibility of creating an object out of the calling code and into a dedicated function, method, or class. Instead of writing new PdfExporter() or new CsvExporter() directly, calling code asks a factory for "an exporter for this format," and the factory decides which concrete class to instantiate. The value is not the extra layer of indirection for its own sake — it is that the decision logic for which class to create lives in exactly one place instead of being duplicated at every call site.
What changed in 2026
- Dependency injection frameworks absorbed a lot of factory pattern's job. Many containers now let you register creation logic once and have it invoked automatically, which covers cases that used to require a hand-written factory class.
- Functional factories became more common than class-based ones. In languages with first-class functions, a factory is often just a function returning an object literal or closure, skipping the class ceremony the original Gang of Four examples used.
- Type systems made factories safer to extend. Strong typing and exhaustiveness checks catch the case where a new type is added to a factory's switch statement but a handler is forgotten — a common source of factory-pattern bugs.
The three flavors
// Simple factory — one function, no subclassing
function createExporter(format: string) {
if (format === "pdf") return new PdfExporter();
if (format === "csv") return new CsvExporter();
throw new Error("unknown format");
}
// Factory method — each subclass overrides how it creates
abstract class ReportGenerator {
abstract createExporter(): Exporter;
generate() {
const exporter = this.createExporter();
exporter.write(this.data());
}
}
Simple factory is a single function with a decision inside it — not officially a Gang of Four pattern, but the version most developers reach for first. Factory method pushes the decision into a subclass, so each subclass decides what to create as part of overriding a method. Abstract factory goes a step further and creates families of related objects together, so a UI theme factory might produce a matching button, checkbox, and dialog all from the same theme.
When it earns its complexity
A factory is worth the extra layer when the decision of which class to instantiate depends on something that varies — user input, configuration, environment, or a previous step's result. If there is only ever one concrete implementation, a factory adds ceremony without adding value; just call the constructor directly.
Factory variants compared
| Variant |
Decision made by |
Adds new types by |
Best for |
| Simple factory |
One function with branching logic |
Editing the function |
Small number of stable types |
| Factory method |
A subclass override |
Adding a new subclass |
Framework code that others extend |
| Abstract factory |
A factory interface implementation |
Adding a new factory implementation |
Families of related objects (themes, platforms) |
| Dependency injection container |
Container configuration |
Registering a new binding |
Apps already using a DI framework |
Common pitfalls
- Overusing factories for objects with no real variation. If a class only ever has one implementation and no plausible second one on the horizon, a factory is speculative complexity.
- Letting the factory become a god object. A factory that knows about every type in the system and has grown a long if/else chain is a sign the types should be registered rather than hardcoded.
- Confusing factory pattern with the singleton pattern. They solve different problems — factory decides which class to instantiate, singleton restricts how many instances exist. They can be combined, but neither implies the other.
FAQ
Is factory pattern the same as a constructor?
No. A constructor builds one specific type. A factory chooses between multiple possible types (or configurations of a type) based on some input, and callers do not need to know the specific class name.
When should I use abstract factory instead of factory method?
Reach for abstract factory when you need to create several related objects that must stay consistent with each other, like a full set of UI components matching one visual theme. Factory method is simpler and fits a single object with per-subclass creation logic.
Does the factory pattern hurt performance?
Negligibly in almost all cases — it is one extra function call or object lookup. The cost that matters is design complexity, not runtime overhead.
Do I need factory pattern if I already use dependency injection?
Often not for simple cases — the DI container can handle straightforward creation. Factories still help when creation logic needs runtime branching that a container's static configuration cannot express cleanly.
Where to go next