Behavior pattern, designed to allocate responsibilities between algorithms and objects, not only describes the pattern of objects or classes, but also describes the communication modes between them, it depicts complex control flows that are difficult to trace during running, and they shift your attention from the control flow to the relationship between objects.
There are 11 common behavior patterns:
1. The behavior-type model uses an inheritance mechanism to allocate behavior among classes:
A. templatemethod)
Template Method mode: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Structure:
Example: If you copy an incorrect question, it will be useless.
Description: The template method mode is composed of an abstract class, which defines the template methods that may be implemented differently. Each specific class derived from this abstract class will implement a new method for this template. Implement code reuse.
B. interpreter mode (Interpreter)
Interpreter mode: for a given language, define a representation of its syntax and define an interpreter which uses this representation to interpret sentences in the language.
Structure:
Example: in fact, you do not understand the boss's mind
Explanation: if a specific type of problem occurs frequently enough, you can consider expressing each instance of the problem as a sentence in a simple language. That is to say, by building an interpreter, the interpreter interprets these sentences to solve the problem.
2. Behavior Object Mode: Compound objects instead of inheritance are used to describe how a group of peer-to-peer objects collaborate to accomplish anything that any object cannot accomplish independently.
A. Mediator)
Intermediary mode: a mediation object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.
Structure:
Example: The world needs peace
Explanation: the collective action is encapsulated into a single intermediary object to avoid this problem. The intermediary is responsible for controlling and coordinating the interaction between a group of objects. The intermediary acts as an intermediary so that the objects in the Group no longer show references to each other. These objects only know the intermediary, thus reducing the number of mutual connections.
B. chain of responsibility)
Responsibility Chain Mode: Enables multiple objects to process requests to avoid coupling between request senders and receivers. Connect the object to a chain and pass the request along the chain until an object processes it.
Structure:
Example: The boss must approve the salary increase
Explanation: We sometimes encounter this situation, that is, there are multiple objects that can process a request. The object that processes the request is unknown in advance and must be automatically determined at runtime. At this time, the best way is to separate the request sender from the specific handler, so that the customer can submit a request without specifying the receiver, and then connect all the objects that can process the request to a chain, and pass the request along this chain until an object processes it.
C. Strategy)
Rule mode: defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode changes the algorithm and does not affect the customers who use the algorithm.
Structure:
Example: Store cash register software
Explanation: inheritance provides a method that supports multiple algorithms or actions. We can directly generate sub-classes B, C, and D of Class A to give it different actions. However, this will combine the implementation of algorithms with the implementation of Class A, which makes it hard to understand, maintain, and expand Class, in addition, algorithms cannot be dynamically changed. After careful analysis, we will find that the only difference between them is the algorithm or behavior they use. They encapsulate the algorithm in an independent strategy Strategy class so that you can change it independently of its class, it is easy to switch, understand, and expand. Obviously, object combinations are better than class inheritance.
3. The behavior Object Mode encapsulates the behavior in an object and assigns the request to it.
A. Command)
Command mode: encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.
Structure:
Example: Thinking about kebabs
Explanation: separating request senders from specific implementers is to decouple the objects that call an operation from the objects that guide how to implement this operation, you can specify, sort, and execute requests at different times, and cancel/Redo operations are supported. Logs of the entire operation can also be recorded, and transactions are supported.
B. iterator)
Iterator mode: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
Structure:
Instance: Want to go? Yes! Buy tickets first
Explanation: the key idea of the iterator mode is to separate list access and traversal from List objects and put them into an iterator object, the iterator class defines an interface to access the list element. The iterator object tracks the current element and knows which elements have been traversed.
C. Memento)
Memo mode: capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
Structure:
Instance: The status value before the game is restored.
Explanation: Using memos can avoid exposing information that must be managed by object a but must be stored outside object. The memorandum mode shields the internal information of object A, which may be very complex, from other objects, thus retaining the encapsulation boundary.
D. The observer mode is also called the publish-subscribe mode (publish/subscribe)
Observer mode: defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of the subject object changes, it notifies all the observer objects so that they can automatically update themselves.
Structure:
Instance:-The boss is back. I don't know
Explanation: The Observer mode focuses on removing the tight coupling between objects. The object and the observer are not closely coupled. They can belong to different abstract layers in a system, the object only knows that it has a series of observers. Each Observer implements a simple interface of the observer. The observer belongs to a specific class and the object is unknown.
E. State)
State mode: allows an object to change its behavior when its internal state changes. This object seems to have changed its class.
Structure:
Instance: when to stop working overtime (work status, off-duty status)
Explanation: the State mode provides a better way to organize Code related to a specific State. The logic for determining the State transfer is not in the IF or switch of a single block, it is distributed among State subclasses. Because all State-related code exists in a state subclass, it is easy to add new States and transformations by defining a new subclass.
F. Visitor mode (visitor)
Visitor mode: indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.
Structure:
Example: men and women
Explanation: it is difficult for visitors to add specific elements, but it is easy to add operations on components that depend on complex object structures. You only need to add a new visitor to define a new operation in an object structure.
Link: http://blog.csdn.net/caozhangyingfei0109/article/details/8614478
Author: Zhang Bo, ninth Information Technology Improvement Course of Langfang Normal University