Patterns in practice
Identify patterns in real codebases, recognize anti-patterns, and learn to combine patterns effectively.
Spotting patterns in real code
Design patterns aren't academic exercises — they're embedded in the tools you use daily. Recognizing them accelerates learning new frameworks and improves your ability to reason about unfamiliar codebases.
| Framework / Library | Pattern | Where |
|---|---|---|
| React | Observer | useEffect dependencies, Context consumers |
| Express / Koa | Strategy | Middleware chain (swappable handlers) |
| Node.js Streams | Iterator + Observer | Readable streams emit data events |
| Prisma ORM | Adapter | Database driver abstraction layer |
| Angular | Decorator | @Component, @Injectable metadata |
| Redux | Command + Observer | Actions as commands, store.subscribe as observer |
Redux dispatches actions (objects describing what happened) and notifies subscribers when state changes. Which two patterns does this combine?
Anti-patterns and common misuse
An anti-pattern is a commonly used solution that appears beneficial but is actually counterproductive. Many anti-patterns arise from misapplying legitimate patterns — using them in the wrong context or taking them too far.
The most dangerous misuse happens when patterns are applied speculatively (before the problem exists) or dogmatically (because a book says so, not because the code demands it).
God Object
A Facade or Singleton that accumulates too many responsibilities. Fix: decompose into focused facades or services.
Cargo Cult Patterns
Applying patterns because they're famous, not because the problem warrants them. Fix: start simple, refactor toward patterns as pain emerges.
Golden Hammer
Treating one pattern as the solution to everything ('everything is a Strategy!'). Fix: evaluate each problem independently.
Lava Flow
Dead code and abandoned patterns left behind after incomplete refactors. Fix: remove dead code aggressively; don't leave half-applied patterns.
What is a 'Cargo Cult Pattern'?
Combining patterns
Production systems rarely use a single pattern in isolation. Effective designs compose patterns to address multiple concerns simultaneously. The skill isn't knowing individual patterns — it's recognizing when and how to combine them.
Common combinations arise naturally: Composite + Iterator (traverse tree structures), Command + Composite (macro commands that group sub-commands), Observer + Strategy (notify observers with pluggable notification strategies), Decorator + Strategy (stack behaviors that delegate to swappable algorithms).
| Combination | Use Case | Example |
|---|---|---|
| Composite + Iterator | Traverse hierarchical structures | File system browser, org chart renderer |
| Command + Composite | Group commands into macros | Text editor macro recording, batch operations |
| Observer + Strategy | Pluggable notification delivery | Alert system: email, SMS, push via strategy |
| Decorator + Adapter | Wrap and adapt simultaneously | Logging adapter that also adds retry behavior |
| Factory + Strategy | Create strategies dynamically | Payment processor selecting strategy by config |
A text editor supports recording macros (sequences of commands that can be replayed). Which pattern combination makes this possible?