description:Guidance on Gang of Four design patterns and modern alternatives — when to use each pattern, how to implement it correctly, and when to avoid it. Use when designing class structures, solving recurring design problems, refactoring switch/if-else chains. Detects from code context and project files — not dependent on specific language keywords."
| **Facade** | Simplify a complex subsystem | Single entry point to complex system |
| **Flyweight** | Many fine-grained objects are too expensive | Share intrinsic state across instances |
| **Proxy** | Control access to another object | Lazy loading, caching, access control, logging |
**Modern alternative:** Middleware chains (e.g., Hono/Express middleware) are a functional take on Decorator. Proxy is often handled by AOP or proxy libraries.
## Behavioral Patterns
| Pattern | When | Why |
|---------|------|-----|
| **Strategy** | Multiple algorithms for the same task | Pass behavior as a parameter |
| **Observer** | One-to-many dependency where state changes need notification | Event emitters, pub/sub |
**Modern alternative:** Strategy → pass a lambda/closure. Observer → use reactive streams or event emitters. Command → functions are commands.
## Pattern Selection Guide
Ask these questions:
1.**What is changing?** Encapsulate what varies.
2.**What's the axis of change?** More types (OO) or more operations (functional)?
3.**Is there a simpler alternative?** A function parameter is often enough.
4.**Does the pattern add clarity or complexity?** Patterns justify themselves only if they reduce overall complexity.
## Modern Patterns (Post-GoF)
- **Dependency Injection** — pass dependencies in, don't create them internally
- **Repository** — abstraction over data access (not in GoF, but ubiquitous)
- **CQRS** — separate read and write models
- **Event Sourcing** — store events, derive state
- **Saga** — orchestrate distributed transactions
- **Circuit Breaker** — fail fast when downstream fails
## Deeper Reference
When the task calls for it, load:
- **[references/catalog.md](references/catalog.md)** — Full GoF pattern catalog with code examples, real-world usage, modern alternatives, and "when to NOT use" guidance for each pattern.