Behavioral patterns are concerned with how objects communicate and how responsibilities are distributed among them. The Observer pattern defines a one-to-many dependency between objects so that when one object, the subject, changes state, all of its dependents, the observers, are notified automatically. This pattern shows up in GUI event listeners, publish-subscribe messaging systems, and reactive programming libraries. It is the right choice when changes in one object require updating an unknown number of others, when the subject should not be tightly coupled to those it notifies, and when an event-driven architecture is desired, as in MVC model-view updates or message brokers.
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, letting the algorithm vary independently from the clients that use it. A context object holds a reference to a strategy and delegates the actual work to it; clients can swap strategies at runtime. This eliminates the conditional statements that would otherwise be needed to select behavior and makes it easy to add new algorithms without modifying existing code, with sorting strategies, payment processing options, and compression methods as common examples. The State pattern is structurally similar but solves a different problem: it allows an object to alter its behavior when its internal state changes, making the object appear to change its class. A TCP connection, for example, behaves differently when established, listening, or closed; each state is a separate class implementing a common interface, replacing complex if/else or switch statements with polymorphism. In Strategy, the client chooses which algorithm to use and the strategies are typically stateless; in State, the object itself transitions between states, and states often know about each other.
The Command pattern encapsulates a request as an object, letting clients be parameterized with different requests and supporting queuing, logging, scheduling, and undo. A typical command interface exposes execute and undo methods, and concrete commands know how to perform and reverse their actions. Common uses include undo and redo functionality in editors, transaction rollback in databases, macro recording, and remote control buttons mapped to device operations. The Template Method pattern defines the skeleton of an algorithm in a base class, deferring some steps to subclasses that override specific steps without changing the algorithm's overall structure. The base class controls the flow and calls subclass hooks, embodying the Hollywood Principle, often phrased as "don't call us, we'll call you."
The Iterator pattern provides a way to access elements of a collection sequentially without exposing its underlying representation, offering a uniform traversal interface that supports multiple simultaneous traversals and hides internal data structure complexity. The Mediator pattern encapsulates how a set of objects interact, with a mediator object handling communication between them so that participants do not refer to each other directly; an air traffic control tower is a typical example, turning many-to-many relationships into simpler one-to-many ones. The Chain of Responsibility pattern passes a request along a chain of handlers, where each handler either processes the request or forwards it to the next; middleware in web frameworks like Express and Laravel is a familiar application, with senders decoupled from receivers and processing chains composed dynamically. The Visitor pattern lets you add new operations to existing object structures without modifying the classes of the elements on which it operates, relying on double dispatch in which an element accepts a visitor and then calls the visitor's method for its own type; this makes adding new operations easy but adding new element types expensive, because every visitor must be updated. Finally, the Memento pattern captures and externalizes an object's internal state so it can be restored later without violating encapsulation: an originator produces state snapshots stored in mementos that a caretaker manages, supporting undo features, game checkpoints, and transaction rollback.