# Design Patterns Quick Reference

## Pattern Overview

| Pattern | Problem | Solution |
|---------|---------|----------|
| Singleton | One instance | Static getInstance, private constructor |
| Factory | Creation by type/config | Centralized creation function |
| Observer | One-to-many updates | Subscribe/notify |
| Strategy | Pluggable algorithm | Delegate to strategy object |
| Decorator | Stackable behavior | Wrap and delegate |
| Adapter | Interface mismatch | Translate interface |

## When to Use

| Need | Pattern |
|------|---------|
| Same instance everywhere | Singleton |
| Create X based on input | Factory |
| Notify listeners on change | Observer |
| Swap algorithm at runtime | Strategy |
| Add behavior without subclassing | Decorator |
| Use legacy/wrong API | Adapter |

## Key Code Shapes

**Strategy:** Client holds strategy, calls strategy.do()

**Observer:** Subject has subscribe/emit; observers register callbacks

**Decorator:** Wraps inner, adds behavior, delegates

**Adapter:** Implements target interface, delegates to adaptee

## Decision Flow

```
Creation logic? → Factory
One instance? → Singleton
Notify many? → Observer
Switch algorithm? → Strategy
Add behavior? → Decorator
Wrong interface? → Adapter
```
