f808abff86
Fix JSON syntax errors in .devcontainer/devcontainer.json (removed
invalid JS-style // comments) and .devcontainer/opencode.json (removed
90+ trailing commas). Apply auto-fixes for end-of-file and trailing
whitespace issues across 100+ files. Fix SIM105 ruff violations in
benchmarks/core_circuit_breaker_bench.py (use contextlib.suppress).
Note: The security fix from issue #7478 (validate_path startswith bypass)
was already delivered to master in commit e18ac5f2. This PR as currently
structured is non-atomic (35 commits across 10+ issues) and needs
significant restructure before merge. This commit only addresses the
CI/pre-commit failures.
ISSUES CLOSED: #7478
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 — Support ticket escalation L1→L2→L3→Manager
- command.md — Text editor with Type/Delete + undo/redo
- interpreter.md — Simple arithmetic expression evaluator
- iterator.md — Fibonacci generator (language-idiomatic)
- mediator.md — Chat room with users communicating through room
- memento.md — Text editor with save/undo snapshots
- observer.md — Weather station with display observers
- state.md — Document workflow: Draft→Review→Published
- strategy.md — Payment processing: CreditCard, PayPal, Crypto
- template-method.md — Data mining with customizable steps
- visitor.md — AST nodes with PrintVisitor and EvalVisitor
- null-object.md — ConsoleLogger vs NullLogger