Behavioral Patterns
Event-Driven Patterns
One-to-many dependency where observers automatically receive notifications when subject state changes, enabling pub/sub and reactive systems.
- Loose coupling between subject and observers
- Event handling and propagation
- Reactive system foundation
- GUI framework backbone
- Event listeners (DOM events)
- Reactive streams (RxJS, React)
- MVC model updates
- Publish-subscribe systems
Centralized communication hub that reduces coupling between objects by coordinating their interactions through a single mediator.
- Simplifies complex object protocols
- Centralizes communication control
- Makes dependencies explicit
- Reduces many-to-many to one-to-many
- Chat room coordination
- Air traffic control systems
- UI form validation
- Event bus implementations
Request handling chain where each handler decides to process the request or pass it to the next handler in sequence.
- Decouples senders from receivers
- Flexible handler assignment
- Sequential processing pipeline
- Dynamic chain composition
- Middleware pipelines (Express)
- Logging level handlers
- Event propagation (bubbling)
- Approval workflow systems
Stores state changes as sequence of events rather than current state, enabling full audit trail and time travel debugging.
- Complete audit history
- Event replay capability
- Temporal queries support
- Event-driven architecture foundation
- Financial transaction systems
- Version control systems
- Audit logging requirements
- CQRS implementations
State & Strategy Patterns
Object behavior changes based on internal state, with state-specific behavior encapsulated in separate state classes.
- State-specific behavior encapsulation
- Eliminates conditional statements
- Explicit state transition logic
- State classes for clarity
- TCP connection states
- Vending machine logic
- Workflow engine states
- Game character states
Defines interchangeable algorithm family with runtime algorithm selection, favoring composition over inheritance.
- Algorithm family encapsulation
- Composition over inheritance
- Open/closed principle adherence
- Runtime algorithm swapping
- Sorting algorithms selection
- Compression strategies
- Payment method selection
- Route planning algorithms
Defines algorithm skeleton in base class with subclasses overriding specific steps, following Hollywood Principle.
- Code reuse through inheritance
- Hooks for customization
- Hollywood principle (don't call us)
- Invariant parts in superclass
- Framework lifecycle hooks
- Data processing pipelines
- UI component rendering
- Parser implementations
Command & Behavioral Patterns
Encapsulates requests as objects, enabling parameterization, queueing, logging, and undo/redo operations.
- Undo/redo functionality
- Macro command composition
- Transactional behavior support
- Delayed execution capability
- UI action handlers
- Job queue systems
- Transaction management
- Database migration tools
Provides sequential access to collection elements without exposing underlying structure, supporting multiple traversals.
- Uniform traversal interface
- Multiple concurrent iterations
- Lazy evaluation support
- Internal structure hiding
- For-each loops (language built-in)
- Database cursors
- Stream APIs (Java, Node)
- Pagination implementations
Separates algorithms from object structure through double dispatch, enabling new operations without changing element classes.
- New operations without class changes
- Double dispatch mechanism
- Type safety preservation
- Operation consolidation
- Compiler AST traversal
- Export to different formats
- Tax calculation systems
- Validation frameworks
