Behavior patterns. Overview
1. Features of building patterns of behavior. Class-level and object-level patterns
The main feature of patterns of behavior is the distribution of responsibilities between objects interacting with each other in different ways. Behavior patterns are characterized by the implementation of a complex control flow, on which the main focus is. During program execution, it becomes difficult to trace the execution of this thread. The links between objects are fading into the background.
Among the patterns of behavior, two groups of patterns are distinguished:
- class-level patterns. In these patterns, the distribution of behavior between different classes is provided using the inheritance mechanism;
- object-level patterns. These patterns are based on the principle of composition. To achieve a solution to the problem, the interaction between a set of objects with the same rights is combined.
The following characteristic features can be distinguished for class-level patterns:
- step-by-step definition of an algorithm using primitive or abstract operations that complete the algorithm in subclasses at the lower levels of the hierarchy (Template Method pattern);
- representation with the help of the class hierarchy of the corresponding grammar of the language.
This grammar is used to interpret the set of operations performed on objects (instances) of these classes (Interpreter pattern).
Object level patterns focus on:
- the way of obtaining information by equal objects;
- joint combination of efforts between objects to achieve the solution of the problem;
- providing service information about the existence of objects that process a request;
- reducing the degree of connectivity of the system of objects (patterns Mediator, Chain of Responsibility);
- control over dependencies between objects (Observer pattern);
- features of behavior inside the object and passing of requests to it;
- features of the implementation of the algorithm inside the object in order to simplify and replace it (Strategy pattern);
- presentation of a request in the form of an object for the purpose of passing it as a parameter, saving, etc. (Command pattern);
- the formation of the state of the object in such a way that when this state changes, a change in the behavior of the object occurs (State pattern);
- providing the necessary behavior of the object in order to avoid the distribution of this behavior between different classes (Visitor pattern);
- providing the necessary way to access objects and a way to traverse these objects from some aggregate (Iterator pattern).
2. List of behavior patterns
Among the patterns of behavior of objects, the following are distinguished:
- Chain of Responsibility – used to create a chain of recipient objects (handlers) that receive and process some request from the client;
- Command (Command, Action, Transaction) – used to send requests to unknown application objects by converting the request itself into an object. This object can be stored like any other;
- Iterator (Cursor) – represents a way to access a complex object. In this case, the internal composition of the object is not highlighted;
- Mediator – implements an object that describes how different objects interact. There are no references to each other between objects. All interaction takes place through an intermediary object;
- Memento (Keeper, Token) – allows you to fix (get) the current internal state of the object in cases where you need to restore the previous state of the object in the event of failures or errors;
- Observer (Observer, Dependents, Publish-Subscribe) – provides a mechanism for notifying interconnected objects in the event that the state of one of these objects changes (all other objects receive the corresponding information);
- State – provides a change in the behavior of the object depending on the state. In this case, the state of an object is determined by a separate class that forms the independence of this object;
- Strategy (Strategy, Policy) – forms the choice of the desired algorithm from the set, depending on the needs of the client. Implements interchangeability of encapsulated algorithms;
- Visitor – implements the description of the operation performed with each object included in some structure. Object classes do not change when a new operation is created.
Class behavior patterns include:
- Interpreter – for a given language, generates sentences that define the grammar of that language based on the use of abstract syntax trees;
- Template Method – defines the basic component of the algorithm, some branches of which can be changed in subclasses without distorting the general structure of this algorithm.