Topic 047 / Core
Creational, structural and behavioral design patterns
Chapter 4.3 · Design Patterns and Data Access
What and why
Certain object-collaboration shapes recur across unrelated domains — creating objects flexibly, adapting a mismatched interface, swapping an algorithm at runtime — and without shared names for these shapes, every team reinvents and re-explains them. Design patterns are named, reusable solutions to these recurring shapes, grouped by what they address: creational (how objects get created), structural (how objects are composed), behavioral (how objects communicate and vary behavior). Builds on: → 44 (most patterns are a SOLID principle applied to one specific recurring shape — Strategy is Open/Closed via composition).
Diagram
| Group | Pattern | Problem it names |
|---|---|---|
| Creational | Factory Method | Let a subclass decide which concrete type to create |
| Creational | Builder | Assemble a complex object step by step |
| Creational | Singleton | Exactly one instance — usually a disguised global; prefer dependency injection |
| Structural | Adapter | Make an incompatible interface fit the one callers expect |
| Structural | Facade | Give a simple entry point to a complex subsystem |
| Structural | Decorator | Add behavior to an object without changing its class |
| Structural | Proxy | Stand in for another object to control access to it |
| Behavioral | Strategy | Swap an algorithm at runtime behind one interface |
| Behavioral | Observer | Notify dependents when state changes, without coupling to them |
| Behavioral | Template Method | Fix an algorithm's skeleton, let subclasses vary one step |
| Behavioral | Command | Turn a request into an object that can be queued, logged, or undone |
| Behavioral | State | Let an object change its behavior as its state changes (→ 46) |
How it works
- Name the shape of the problem — varying creation, a mismatched interface, a swappable algorithm — before reaching for a named pattern.
- Pick the smallest pattern that fits; most problems need one pattern, not a stack of them.
- Implement the interface the pattern defines; keep each concrete class focused on one variant.
- Confirm the pattern earns its complexity against the three standard questions (when to use it, when not to, the simpler alternative) before committing to it.
Practical example
(illustrative). A payment module with if (provider == "stripe") ... else if (provider == "paypal") ... becomes a PaymentStrategy interface with one
implementation per provider, selected by a PaymentStrategyFactory — Strategy for the
swappable charging logic, Factory Method for choosing which one to construct.
When to use it
- Use when: behavior genuinely varies today, or a second/third variant is a documented near-term requirement.
- Avoid when: a pattern is applied "because it's good practice" with only one implementation ever expected — Singleton is the most abused example of this; dependency injection of a plain instance is almost always the better fit.
- Failure scenarios: a pattern zoo — a simple module turned into five collaborating
classes nobody can trace end to end for a problem that never needed more than one
if. - Simpler alternative: an
if/elseorswitchfor two or three stable variants beats Strategy + Factory until a third or fourth variant actually arrives.
Practice
(20–30 min; no lab services — any language; merges this chapter's
exercise). Refactor a payment module from an if/else chain to Strategy + Factory
Method, supporting three providers; then write down, concretely, what the plain
if/else would have cost (editing and re-testing this method for every new provider)
and what it would have saved (no interface, no factory, no extra files) for exactly three
providers. Done when adding a fourth provider requires adding one new class and editing
nothing that already exists.
Key takeaway
Patterns are names for recurring collaboration shapes, applied only when the shape is real. Done when you can: name the pattern a piece of code is using and state what the simpler alternative would have cost. Used next in → 48 (repository is itself a structural, collection-like pattern), → 151 (circuit breaker, a resilience pattern), → 155 (CQRS separates read/write patterns at architecture scale).
References
None — this topic makes no external claims.
Source directory