# Behavioral Design Patterns

Behavioral patterns manage algorithms, relationships, and responsibilities between objects. They focus on **how objects communicate** and **how responsibilities are distributed**.

## All 12 Behavioral Patterns (+ Null Object)

| Pattern | Intent | Key Mechanism | Coupling |
|---|---|---|---|
| **Chain of Responsibility** | Pass request along a chain of handlers | Linked handlers | Low — sender doesn't know which handler processes |
| **Command** | Encapsulate a request as an object | Command objects with execute/undo | Low — invoker decoupled from receiver |
| **Interpreter** | Define a grammar and evaluate sentences | Recursive AST evaluation | Medium — tightly coupled to grammar |
| **Iterator** | Traverse a collection without exposing internals | next/hasNext interface | Low — collection internals hidden |
| **Mediator** | Centralize complex communication | Central mediator object | Low between peers; high to mediator |
| **Memento** | Capture and restore object state | Opaque snapshot objects | Low — caretaker can't modify memento |
| **Observer** | Notify dependents of state changes | Subject/subscriber registry | Low — subject doesn't know concrete observers |
| **State** | Change behavior when internal state changes | State objects with transitions | Medium — states know about each other |
| **Strategy** | Swap algorithms at runtime | Interchangeable strategy objects | Low — client picks strategy |
| **Template Method** | Define skeleton, let subclasses fill steps | Abstract methods in base class | Medium — subclass coupled to template |
| **Visitor** | Add operations to objects without modifying them | Double dispatch (accept/visit) | High — visitor knows all element types |
| **Null Object** | Provide a do-nothing default to avoid null checks | No-op implementation of interface | Low — transparent substitute |

## Decision Tree

```
Need to handle a request?
├─ One handler is enough, but which one is unknown → Chain of Responsibility
├─ Need to queue, log, or undo requests → Command
└─ Need to evaluate structured expressions → Interpreter

Need to traverse a collection?
└─ Want uniform iteration without exposing internals → Iterator

Need objects to communicate?
├─ Many-to-many communication is getting tangled → Mediator
└─ One-to-many: state change should notify others → Observer

Need to manage object state?
├─ Object behavior changes based on state → State
├─ Need to save/restore snapshots → Memento
└─ Need to eliminate null checks → Null Object

Need to vary behavior?
├─ Swap entire algorithm at runtime → Strategy
├─ Fixed skeleton, varying steps → Template Method
└─ Add operations to a class hierarchy without changing it → Visitor
```

## Pattern Relationships

```
Command ←→ Memento        (Command stores memento for undo)
Observer ←→ Mediator      (Mediator often uses Observer internally)
State ←→ Strategy         (Both delegate to composed objects; State allows transitions)
Iterator ←→ Visitor       (Visitor can use Iterator to traverse)
Chain ←→ Command          (Chain handlers can be commands)
Template ←→ Strategy      (Template uses inheritance; Strategy uses composition)
```

## When to Prefer Composition (Strategy) vs Inheritance (Template Method)

| Factor | Strategy (Composition) | Template Method (Inheritance) |
|---|---|---|
| Algorithm varies independently | Yes | No |
| Need to swap at runtime | Yes | No |
| Multiple varying dimensions | Yes — compose strategies | No — combinatorial explosion |
| Fixed skeleton with hook points | No | Yes |
| Language lacks interfaces | Template Method works | N/A |

## Files in This Directory

- [chain-of-responsibility.md](chain-of-responsibility.md) — Support ticket escalation L1→L2→L3→Manager
- [command.md](command.md) — Text editor with Type/Delete + undo/redo
- [interpreter.md](interpreter.md) — Simple arithmetic expression evaluator
- [iterator.md](iterator.md) — Fibonacci generator (language-idiomatic)
- [mediator.md](mediator.md) — Chat room with users communicating through room
- [memento.md](memento.md) — Text editor with save/undo snapshots
- [observer.md](observer.md) — Weather station with display observers
- [state.md](state.md) — Document workflow: Draft→Review→Published
- [strategy.md](strategy.md) — Payment processing: CreditCard, PayPal, Crypto
- [template-method.md](template-method.md) — Data mining with customizable steps
- [visitor.md](visitor.md) — AST nodes with PrintVisitor and EvalVisitor
- [null-object.md](null-object.md) — ConsoleLogger vs NullLogger
