When to use
When you need to manage algorithms, responsibilities, and communication between objects while keeping them loosely coupled.
Key traits
- Focus on object collaboration and delegation
- Encapsulate behavior variations
- Define protocols for object interaction
- Enable runtime behavior changes
Common Scenarios
| Pattern | When to Use |
|---|---|
| ๐ฏ Strategy | Selecting algorithms at runtime or swapping business logic implementations |
| ๐๏ธ Observer | Event-driven systems where multiple objects need to react to state changes |
| ๐ Command | Encapsulating requests for undo/redo, queuing, or logging operations |
| ๐ Template Method | Defining algorithm skeleton while letting subclasses override specific steps |
| ๐ State | Changing object behavior based on its internal state (state machines) |
| โ๏ธ Chain of Responsibility | Processing requests through a chain where any handler can process or pass along |
| ๐ Iterator | Accessing elements of a collection sequentially without exposing its structure |
| ๐ค Mediator | Centralizing complex communications and dependencies between objects |
| ๐ถ Visitor | Adding new operations to existing object structures without modifying them |
| ๐พ Memento | Capturing and restoring object state for undo/rollback functionality |
| ๐ Interpreter | Implementing domain-specific languages or parsing expression grammars |