ArticleDirectory
He first described the chain of responsibility mode in his book "Java and patterns:
The responsibility chain mode is an object behavior mode. 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 and assign responsibilities 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.
For example, Jia Mu, Jia Yi, Jia Zheng, Jia Baoyu, and Jia Huan are the five painters who participate in the drum and flower games. They form a chain link. The tambourine passes the flowers to Jia mu to start the flower games. The flowers are transmitted from Jia mu to Jia Yu, and from Jia Zheng to Jia Baoyu, and from Jia Baoyu to Jia Huan, and from Jia Huan to Jia Mu, as shown in. When the drums stop, the flowers in their hands will have to execute the command.
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 following describes the simplest implementation of the responsibility chain model.
The roles involved in the responsibility chain mode are as follows:
●Abstract handler role:Define 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 a Java Abstract class or Java interface. The aggregate relationship of the handler class in gives reference to the sub-class. The abstract method handlerequest () standardizes the operations of the sub-class to process requests.
●The specific handler (concretehandler) role:After receiving the request, the specific handler can choose to process the request or send the request to the next house. Because the specific Handler holds a reference to the next home, if necessary, the specific handler can access the next home.
Source code
Abstract processor role
Public Abstract Class Handler { /** * Objects with subsequent responsibilities */ Protected Handler successor; /** * Indicates the method used to process the request. Although this method does not include parameters *, you can actually pass in parameters. Select whether to pass parameters as needed. */ Public Abstract Void Handlerequest (); /** * Value Method */ Public Handler getsuccessor (){ Return Successor ;} /** * Assign a value to set the next responsibility object */ Public Void Setsuccessor (handler successor ){ This . Successor = Successor ;}}
Specific handler role
Public Class Concretehandler Extends Handler { /** * Processing method. Call this method to process the request. */ @ Override Public Void Handlerequest (){ /** * Determine whether there are successor responsibility objects * If yes, forward the request to the successor responsibility object * If not, process the request */ If (Getsuccessor ()! = Null ) {System. Out. println ( "Disallow requests" ); Getsuccessor (). handlerequest ();} Else {System. Out. println ( "Processing requests" );}}}
Client type
Public ClassClient {Public Static VoidMain (string [] ARGs ){//Assemble responsibility chainHandler handler1 =NewConcretehandler (); handler handler2=NewConcretehandler (); handler1.setsuccessor (handler2 );//Submit requestHandler1.handlerequest ();}}
It can be seen that the client creates two handler objects and specifies that the next home of the first handler object is the second handler object, while the second handler object is not home. The client then passes the request to the first processor object.
The transfer logic in this example is very simple: as long as there is a next home, it will be passed to the next home for processing; if there is no next home, it will be handled by itself. Therefore, after receiving the request, the first handler object will pass the request to the second handler object. Because the second processor object does not have a home, the request is processed by itself. The following figure shows the activity sequence.
Use Cases
To consider this function: To apply for dinner expenses management.
Many companies have such benefits: the project team or department can apply for dinner expenses from the company to organize members of the project team or department for dinner activities.
The general process for applying for dinner expenses is generally: The applicant first fills in the application form and then submits it to the leader for approval. If the application is approved, the leader will notify the applicant to approve the application, and then the applicant will go to the finance department to collect the fee, if no application is approved, the supervisor will notify the applicant that the application has not been approved.
Different levels of leadership have different approval quotas. For example, the project manager can only approve applications of less than 500 RMB; the department manager can approve applications of less than 1000 RMB; the general manager can review any quota application.
That is to say, when a person applies for dinner expenses, the request will be handled by a leader in the Project Manager, Department Manager, and general manager, however, the applicant does not know who will handle his request. Generally, the applicant submits his application to the project manager, or the general manager may finally process his request.
You can use the responsibility chain mode to implement the above functions: When someone applies for dinner expenses, the request will beProject Manager> Department Manager> General ManagerWhen such a leader processes the link and sends a request, the person who sends the request does not know who will handle the request. Each leader will, according to his/her responsibilities, to determine whether to process the request or send the request to a higher-level leader. As long as a leader processes the request, the transfer is over.
We need to separate the handling of each leader into a separate responsibility handling object, and then provide them with a public and abstract parent responsibility object, in this way, you can dynamically combine the responsibility chain on the client to implement different functional requirements.
Source Code
Abstract processor role
Public Abstract Class Handler { /** * The object holding the next request processing */ Protected Handler successor = Null ; /** * Value Method */ Public Handler getsuccessor (){ Return Successor ;} /** * Set the object for processing the next request */ Public Void Setsuccessor (handler successor ){ This . Successor = Successor ;} /** * Process the application for dinner expenses * @ Param User applicant * @ Param Amount of money requested by the supervisor * @ Return Notification of success or failure */ Public Abstract String handlefeerequest (string user, Double Token );}
Specific handler role
Public class projectmanager extends handler {
@ override
Public string handlefeerequest (string user, double strong) {
string STR =" ";
// the project manager has low permissions and can only be less than 500
If (latency <500 )
{< br> /// simplified