Overview of behavior patterns
1. behavioralpatternDivision of responsibilitiesAndAlgorithmAbstract.
2. The behavior pattern is not only about the structure of struct classes and objects, but also focuses on the interaction between them.
3. Through the behavior mode, you can clearly classify objects and responsibilities, and studyInteraction. Objects are not isolated during system running. They can communicate and collaborate with each other to complete some complex functions. An object also affects the running of other objects during running.
Behavior patterns are divided into two types: behavior patterns and object behavior patterns:
Behavior-like Mode: Class behavior mode usageInheritance relationshipBehavior is distributed among several classes. The class behavior pattern mainly allocates the responsibilities of the parent class and the Child class by means of polymorphism.
Object behavior mode: The behavior mode of the object usesAggregate associationsTo distribute actions. The object behavior pattern mainly allocates two or more classes by means of object Association. According to the "merging Reuse Principle", the system should try to replace the inheritance relationship with the association relationship. Therefore, most of the behavior-oriented design patterns belong to the object behavior-oriented design pattern.
Category:
Chain of responsibility)
Command)
Interpreter mode (Interpreter)
Iterator)
Mediator)
Memento)
Observer mode (observer)
State)
Strategy)
Template Method)
Visitor mode (visitor)
Chainof responsibility pattern)
Model motivation:
1. A responsibility chain can be a straight line, a ring, or a tree structure. The most common responsibility chain is a straight line, that is, requests are transmitted along a one-way chain.
2. Each object in the chain is a request handler. In the responsibility chain mode, the request handler can be organized into a chain and the request is transmitted along the chain, the handler on the chain processes the request accordingly. The client only needs to send the request to the chain without worrying about the request processing details and request transmission, decouples request senders from request handlers. This is the model motivation of the responsibility chain model.
Pattern intent:
Chain of responsibility pattern: Avoid coupling between the request sender and the receiver, so that multiple objects may receive requests and connect these objects into a chain, and pass the request along this chain until an object processes it. Due to English translation differences, the responsibility chain model is also called the responsibility chain model, which is an object behavior model.
UML diagram:
Role:
Handler: the abstract handler defines an interface for processing requests.
Concretehandler: a specific handler is a subclass of an abstract handler. It can process user requests.
Client: customer
Mode Analysis:
1. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain.
2. Requests are transmitted on this chain until an object on the chain processes the request.
3. The client that sends the request does not know which object on the chain finally processes the request, which allows the system to dynamically re-organize the chain and assign responsibility without affecting the client.
Code:
Public abstract class Handler
{
Protectedhandler successor;
Publicvoid setsuccessor (handler successor)
{
This. Successor = successor;
}
Publicabstract void handlerequest (string request );
}
Public class concretehandler extendshandler
{
Publicvoid handlerequest (string request)
{
If (the request meets the conditions)
{
... // Process the request;
}
Else
{
This. Successor. handlerequest (request );//Forward request
}
}
}
Advantages and disadvantages:
Advantages:
1. Reduce Coupling
2. Mutual object connection can be simplified.
3. Enhanced flexibility in assigning roles to objects
4. It is convenient to add a new request processing class.
Disadvantages:
1. The request must be received.
2. the system performance will be affected, and it is not convenient to debug the Code; it may cause loop calls.
Scope of use:
1. There are multiple objects that can process the same request. The specific object that processes the request is automatically determined by the runtime.
2. If the recipient is not explicitly specified, submit a request to one of multiple objects.
3. A group of objects can be dynamically specified to process requests.
Mode extension:
Pure and non-pure responsibility chain model
1. A pure responsibility chain model requires that a specific handler object can only select one of the two actions: one is responsible, and the other is responsible for pushing the responsibility to the next house. It is not allowed that a specific processor object transfers down the responsibility after assuming part of the responsibility.
2. In a pure responsibility chain mode, a request must be received by a handler object. In an unpure responsibility chain mode, A request cannot be received by any receiving end object.