Patterns as Vocabulary, Not Ritual
The pattern shapes you will actually use, learned from the problem each one answers.
How to learn patterns without cargo-culting them
A design pattern is a named, reusable solution shape to a problem that recurs. The name is worth having only after you have felt the problem. So every pattern in this module follows one arc:
- Problem โ the code smells it: duplication, a switch that grows on every change, construction code nobody can follow.
- Naive solution โ what you would write in a hurry.
- What breaks โ the concrete pain that shows up next sprint.
- Pattern โ the shape that names and fixes the pain.
- Tradeoffs โ what the pattern costs (indirection, ceremony, files).
- When NOT to use it โ the most skipped, most valuable part.
The seven you will build
| Pattern | The problem it answers | The tell you need it |
|---|---|---|
| Strategy | one behavior, many interchangeable variants | a growing switch over a "mode" |
| Factory Method | object construction branching on input | new scattered behind ifs |
| Builder | constructing a complex object step by step | constructor with 8 bools/strings |
| Adapter | an interface you need vs an interface you have | a third-party shape leaking in |
| Decorator | layering optional behavior on a core | N boolean flags each wrapping a step |
| Repository | persistence details leaking into logic | SQL in a business method |
| Observer | one event, many reactions, decoupled | publisher calling 5 concrete subscribers |
The universal caution
Patterns add indirection. Indirection is a cost you pay for a benefit you must actually have. "We might need it later" is not a benefit. The honest sequence in real work: write it naive, feel the pain, then reach for the named shape โ which is exactly how this module grades you.