Chain of responsibility Mode
The responsibility chain mode is an object behavior mode [gof95 ]. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The client that sends this request does not know which object on the chain will eventually process this request, which allows the system to dynamically reorganize the chain and assign responsibility without affecting the client.
Starting from drumming and spreading flowers
Drumming is a lively and intense drinking game. At the banquet, guests are arranged in sequence to drum up one person. The drums are separated from the flowers to show justice. When the drum is started, the bouquet begins to pass in sequence, and the sound of a drum falls. If the bouquet is in the hands of someone, the person will have to drink.
It is the application of the responsibility chain model. A responsibility chain may be a straight line, a link chain, or a part of a tree structure.
Structure of the responsibility chain model
The roles involved in the responsibility chain mode are as follows:
Abstract handler role: defines an interface for processing requests. If necessary, the interface can define a method to set and return references to the next house. This role is usually implemented by an abstract class or interface.
Concretehandler role: after receiving a request, the handler can choose to process the request or send the request to the next home. Because the specific Handler holds a reference to the next home, if necessary, the specific handler can access the next home.