Detailed definition
To prevent the request sender from being coupled with the receiver, it is possible for multiple objects to receive the request, connect the objects as a chain, and pass the request along the chain until the object has processed it, and the chain of responsibility pattern, also known as the responsibility chain pattern, is an object-behavior pattern. (If you have been exposed to exception handling, you can better understand it by applying the exception handling mechanism.)
The chain of responsibility can be a straight line, a ring, or a tree structure, but the most common chain of responsibilities is a straight line, which is to pass a request along a one-way chain. Each object on the chain is a request handler, and the responsibility chain pattern organizes the requested processor into a chain and enables the request to be passed along the chain, which is handled by the processor on the chain, and the client does not care about the processing details of the request and the delivery of the request, simply sending the request to the chain. In this way, the sender of the request and the processor of the request are decoupled, and the dependencies between the two roles are eliminated and can be combined freely.
Principle Structure
Illustrates the principle of the implementation of the responsibility-linked model, which includes the following key roles:
- Handler: Abstract processor. Defines an interface for processing requests. If necessary, the interface can define a method to set and return a reference to the home. This role is usually implemented by an abstract class or interface.
- Concretehandler: Specific processor. When the specific processor receives the request, it can choose to dispose of the request or pass the request to the other person. As the specific processor holds references to the homes, the specific processor can access the homes if necessary.
- Client: Clients
- HandleRequest: The public interface of the abstract processor requires that each chained node implement this interface to handle the request data sent by the client.
For each chained node, you need to meet two conditions:
- The abstract interface defined by the abstract processor (Handler) is implemented to recognize the received request;
- There is a successor that is used to forward the current undeliverable request forwarding to the next node so that a chain can be formed. (successor refers to the next Concretehandler reference, which is equivalent to the next pointer in the list)
As a result of the above programming design, so that the request and processing the object of the request has no dependency, because the client does not even know who handled the request, so that the entire chain structure is flexible, can add new nodes at any time, of course, also support the arbitrary adjustment of node sequence, delete unnecessary nodes and so on.
By the above explanation, you may find that the chain structure we are talking about is related to the linked list we are familiar with. If you think the chain of responsibilities is a list, then I can only say that you too young too simple.
Sample code
adjourned
Summary
Behavioral patterns are abstractions that divide responsibilities and algorithms between different objects, and behavioral patterns focus not only on the structure of classes and objects, but also on the interactions between them. With behavioral patterns, you can more clearly divide the responsibilities of classes and objects, and study the interaction of the system between instance objects at run time. Behavioral patterns can be classified into class-behavioral patterns and object-behavioral patterns. The responsibility chain pattern avoids the request sender being coupled with the receiver, allowing multiple objects to receive requests, connecting the objects to a chain, and passing the request along the chain until an object is processed by it, which is an object behavior pattern.
In our daily use, we may not have a lot of direct contact with this aspect, but if you are serious about a processing mechanism that studies the process sequence, then you will be able to discover that this processing mechanism uses the responsibility chain to handle the exception errors thrown in the program.
In the responsibility chain model, many objects are linked together by each object's reference to its next-generation chain. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain is ultimately processing the request, which allows the system to dynamically reorganize the chain and assign responsibility without affecting the client.
The main advantage of the responsibility chain model is that it can reduce the coupling degree of the system, simplify the mutual connection of objects, and enhance the flexibility of assigning responsibilities to the object, and it is convenient to add new request processing classes, the main disadvantage is that the request must be received, and for the long chain of responsibility, the processing of the request may involve multiple processing objects. System performance is affected and is not convenient for code debugging.
Advantages:
- Reduction of coupling
- Simplifies the connection of objects to each other
- Increased flexibility in assigning responsibilities to objects
- It is convenient to add new request processing classes
Disadvantages:
- There is no guarantee that the request must be received.
- System performance is affected and is not convenient for code debugging (may cause circular calls)
Reference
- Three responsibility chain mode of design pattern
- Relive design Patterns (iii)--responsibility chain mode (chain of responsibility)
Responsibility chain mode (Chain of Responsibility)