Behavioral patterns
Define how objects communicate, distribute responsibilities, and encapsulate algorithms.
Observer
The Observer establishes a one-to-many dependency: when the subject changes state, all registered observers are notified and updated automatically. The subject doesn't know the concrete types of its observers — only that they implement a notification interface.
This is the foundation of event-driven architectures, reactive programming, and pub/sub systems. DOM event listeners, RxJS observables, Redux store subscriptions, and Vue's reactivity system are all Observer variants.
What problem does the Observer pattern solve?
Strategy
The Strategy extracts varying algorithms into separate classes that share a common interface. The context holds a reference to a strategy and delegates to it — swapping strategies changes behavior without modifying the context.
This replaces conditional logic (if/else, switch) with polymorphism. Instead of a giant function with branches for each algorithm, each branch becomes its own class implementing the strategy interface.
What does the Strategy pattern replace?
Command
The Command encapsulates a request as an object — bundling the action, its parameters, and the receiver into a single callable entity. This decouples the invoker (who triggers the action) from the receiver (who performs it).
Because commands are objects, you can queue them, log them, serialize them, support undo/redo, or schedule them for later execution. Text editors, task schedulers, and transactional systems rely heavily on this pattern.
What capability does Command unlock that direct method calls cannot?
Iterator
The Iterator provides a way to traverse a collection without exposing its internal structure (array, linked list, tree, hash map). The client gets a uniform traversal interface regardless of how the collection stores its elements.
Modern languages have largely absorbed this pattern into the language itself: JavaScript's `for...of`, Python's `iter()`/`next()`, Java's `Iterable`, C#'s `IEnumerable`. You rarely implement Iterator from scratch anymore — but understanding it explains why these constructs work.
Why has the Iterator pattern been largely absorbed into modern programming languages?
Chain of Responsibility
The Chain of Responsibility pattern lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler.
It is heavily used in UI event handling and middleware pipelines (like Express.js).
Where is the Chain of Responsibility pattern most commonly seen in modern web development?
Interpreter
The Interpreter pattern defines a representation for a grammar as well as a mechanism to understand and execute it.
It is rarely used for complex languages (where parsers and ASTs are better), but useful for simple configuration languages or regular expressions.
For which scenario is the Interpreter pattern most appropriate?
Mediator
The Mediator pattern reduces chaotic dependencies between objects by forcing them to communicate through a central mediator object.
This prevents a tangled web of mutually dependent components, heavily utilized in UI dialogs and chat rooms.
How does the Mediator pattern improve coupling?
Memento
The Memento pattern lets you save and restore the previous state of an object without revealing the details of its implementation.
It is the standard pattern for implementing 'Undo/Redo' functionality.
What is the primary use case for the Memento pattern?
State
The State pattern lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.
It is a cleaner, object-oriented alternative to massive `switch` statements governing state machine transitions.
The State pattern is essentially an object-oriented replacement for what?
Template Method
The Template Method pattern defines the skeleton of an algorithm in a base class but lets subclasses override specific steps of the algorithm without changing its structure.
It leverages inheritance to provide a rigid algorithm framework with customizable hooks.
How does Template Method differ from Strategy?
Visitor
The Visitor pattern lets you separate algorithms from the objects on which they operate. It allows you to add new behaviors to a class hierarchy without altering any existing code.
It relies on a technique called 'Double Dispatch' and is heavily used in compilers for traversing Abstract Syntax Trees (AST).
Which programming technique makes the Visitor pattern possible in languages that don't support it natively?