Handbooks / OOP & Design Patterns / Chapter 6
Creational Patterns
7 pages · ~5 min✓ Reviewed
Builds on Coupling and Cohesion. Next up: Structural Patterns.
Creational Patterns: The Mental Model
1. The Idea
- Decouple what gets created from the code that asks
- Callers depend on an interface, not a concrete class
2. Five Patterns
| Pattern | Core question | Idea |
|---|---|---|
| Singleton | How many? | One instance |
| Factory Method | Which class? | Subclass picks the type |
| Abstract Factory | Which family? | Related objects together |
| Builder | How to assemble? | Step-by-step construction |
| Prototype | Copy from what? | Clone; costly to create, cheap to copy |
3. Spot the Need
new ConcreteClass()scattered around- Constructors with many parameters
- Switch on a type tag
Rule of thumb
Start with a plain constructor. Refactor to a pattern only when the variation actually appears.
4. Interview Point
Singleton and Factory Method
1. Singleton
- Exactly one instance plus a global access point
- Private constructor, static field, static
getInstance()
2. Thread Safety and Pitfalls
| Init | Thread-safe? |
|---|---|
| Lazy, no lock | No: two threads can both create |
| Lazy + lock | Yes |
Double-checked + volatile | Yes |
Abstract Factory and Builder
1. Abstract Factory
- One interface, one create method per product:
createButton(),createCheckbox() - Each concrete factory returns a matching set: DarkThemeFactory, LightThemeFactory
- Use when products belong together and are never mixed across families
- Typical cases: UI themes, cross-platform widgets, per-database DAOs
| Factory | createButton() | createCheckbox() |
|---|---|---|
| DarkThemeFactory | DarkButton | DarkCheckbox |
Prototype and Choosing Between Patterns
1. Prototype
- Prototype: new object by
clone()of an existing instance - Skips costly init: DB loads, heavy config
- Shallow copy shares nested refs; mutating the clone hits the original
- Deep-copy mutable fields; beware circular refs
python
import copy a = {"t": [1]} s, d = a.copy(), copy.deepcopy(a) a["t"].append(2) print(s["t"], d["t"])
shallow copy s sees the change, deep copy d does not
output
[1, 2] [1]
2. Telling Them Apart
| Factory Method |
|---|
Common Mistakes with Creational Patterns
1. Singleton Misuse
- Used as a convenient global: hides dependencies, tests become order-dependent
- Fix: inject the one shared instance
- Lazy init without a lock: two threads can both create it
java
private static volatile Db inst; static Db get() { if (inst == null) synchronized (Db.class) { if (inst == null) inst = new Db(); } return inst; }
double-checked locking
2. Over-Engineering
- Factory or Builder on a simple object: extra indirection, no variation to justify it
3. Copy and Build Bugs
Creational Patterns Cheat Sheet
1. Which Pattern
| Pattern | Asks | Pick when |
|---|---|---|
| Singleton | How many? | Need exactly one |
| Factory Method | Which class? | Class unknown until runtime |
| Abstract Factory | Which family? | Matched families |
| Builder | How to assemble? | Many optional params |
| Prototype | Copy from what? |
Check yourself
A lazy Singleton does if (inst == null) inst = new Config();. Two threads call getInstance() first, at the same time. What can go wrong?
- Both see
null, so two instances get created. - Fix: eager init, an enum singleton, or a lock with a
volatilefield.
You clone an Order whose items list is shared (shallow copy), then add an item to the clone. What does the original hold?
- The new item too: both objects point to one list.
- Deep-copy mutable nested fields.
You add createSlider() to an Abstract Factory that already has DarkThemeFactory and LightThemeFactory. What breaks?
- Every concrete factory must implement it.