11 — Creational Patterns¶
Driving question: When is object creation a design problem rather than a constructor call?
Learning objectives¶
- Compare Factory Method, Abstract Factory, Builder, Prototype, and Singleton by intent.
- Identify creation volatility and configuration coupling.
- Recognize overuse of factories and service locators.
- Refactor creation logic while preserving testability.
Creation as dependency management¶
Creation patterns become useful when clients should not know:
- concrete product classes;
- construction sequence;
- environment-specific configuration;
- product-family consistency rules;
- expensive cloning/initialization details.
Comparison¶
| Pattern | Primary force | Typical liability |
|---|---|---|
| Factory Method | defer creation to subtype/extension point | inheritance coupling |
| Abstract Factory | create compatible product families | interface proliferation |
| Builder | separate complex construction sequence | extra indirection |
| Prototype | create by copying configured exemplar | clone semantics |
| Singleton | one globally accessible instance | hidden global dependency |
Singleton
“Exactly one instance” and “globally reachable from anywhere” are different requirements. The latter often damages testability and dependency visibility.
Design / research exercise¶
Refactor a constructor-heavy object graph into two alternative designs: explicit composition root versus factory abstraction. Compare dependency visibility and test setup.
Suggested reading¶
- GoF, Creational Patterns chapters.