Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
Chain of Responsibility
to pass requests along a chain of handlers
Command
to turn a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.
Iterator
to traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
The main idea of the Iterator pattern is to extract the traversal behavior of a collection into a separate object called an iterator.
Mediator
to reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.
An excellent example of the Mediator pattern is a railway station traffic system. Two trains never communicate between themselves for the availability of the platform. The stationManager acts as a mediator and makes the platform available to only one of the arriving trains while keeping the rest in a queue. A departing train notifies the stations, which lets the next train in the queue to arrive.
Memento
to save and restore the previous state of an object without revealing the details of its implementation.
The Memento pattern delegates creating the state snapshots to the actual owner of that state, the originator object. Hence, instead of other objects trying to copy the editor’s state from the “outside,” the editor class itself can make the snapshot since it has full access to its own state.
The pattern suggests storing the copy of the object’s state in a special object called memento. The contents of the memento aren’t accessible to any other object except the one that produced it. Other objects must communicate with mementos using a limited interface which may allow fetching the snapshot’s metadata (creation time, the name of the performed operation, etc.), but not the original object’s state contained in the snapshot.
Applicability
Use the Memento pattern when you want to produce snapshots of the object’s state to be able to restore a previous state of the object.
Observer
Also known as: Event-Subscriber, Listener
Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.
State
to make an object alter its behavior when its internal state changes. It appears as if the object changed its class.
The State pattern suggests that you create new classes for all possible states of an object and extract all state-specific behaviors into these classes.
Instead of implementing all behaviors on its own, the original object, called context, stores a reference to one of the state objects that represents its current state, and delegates all the state-related work to that object.
Applicability
Use the State pattern when you have an object that behaves differently depending on its current state, the number of states is enormous, and the state-specific code changes frequently.
** The pattern suggests that you extract all state-specific code into a set of distinct classes. As a result, you can add new states or change existing ones independently of each other, reducing the maintenance cost.
Use State when you have a lot of duplicate code across similar states and transitions of a condition-based state machine.
** The State pattern lets you compose hierarchies of state classes and reduce duplication by extracting common code into abstract base classes.
Strategy
to define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
Template Method
to define the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
The Template Method pattern suggests that you break down an algorithm into a series of steps, turn these steps into methods, and put a series of calls to these methods inside a single template method. The steps may either be abstract, or have some default implementation. To use the algorithm, the client is supposed to provide its own subclass, implement all abstract steps, and override some of the optional ones if needed (but not the template method itself).
Visitor
Visitor lets you add “external” operations to a whole class hierarchy without changing the existing code of these classes.
interpreter
provides a way to evaluate and interpret a language grammar or expression