If you organize several objects in a hierarchical structure, such as an organization as a chain of responsibility, client-side code might not have to know in advance which class to use. In this case, each object in the chain has a method that, when the client code calls the method, either executes the method or forwards the method invocation request along the chain.
The responsibility chain pattern allows each object to have an opportunity to decide whether or not to process the request in order to avoid coupling between the sender of the request and its receiver.
The purpose of the responsibility chain model is to relieve the pressure on the caller so that they do not need to know which object can handle the call request.
If you experience the following scenarios during development, consider applying the chain of responsibility Model:
There are multiple objects that can handle a request, which object handles the request when the run time is automatically determined
You want to submit a request to one of multiple objects without explicitly specifying the recipient
see a class diagram of the application responsibility chain pattern
1, define a processing request interface, (optional) implementation of the successor chain
Public interface RequestHandle { void HandleRequest (Request request);
2. Handle the request it is responsible for, and access its successors. If the request can be processed, the request is forwarded to its successor.
public class Hrrequesthandle implements RequestHandle {public void HandleRequest (Request request) { if (request instanceof dimissionrequest) { System.out.println ("to leave, personnel approval!"); } SYSTEM.OUT.PRINTLN ("request Complete");} }
public class Pmrequesthandle implements RequestHandle { RequestHandle RH; Public Pmrequesthandle (RequestHandle RH) { THIS.RH = RH; } public void HandleRequest (Request request) { if (Request instanceof Addmoneyrequest) { System.out.println ("to raise , the project manager approves! "); } else { rh.handlerequest (request);}}}
public class Tlrequesthandle implements RequestHandle { RequestHandle RH; Public Tlrequesthandle (RequestHandle RH) { THIS.RH = RH; } public void HandleRequest (Request request) { if (Request instanceof Leaverequest) { System.out.println ("to leave, Project leader approval! "); else { rh.handlerequest (request);}}}
3. Submit a request to a specific processor (Concretehandler) object on the chain
public class Test {public static void Main (string[] args) { RequestHandle hr = new Hrrequesthandle (); RequestHandle pm = new Pmrequesthandle (HR); RequestHandle tl = new Tlrequesthandle (PM); Team leader processing a request for separation requests = new Dimissionrequest (); Tl.handlerequest (request); System.out.println ("==========="); Team leader to handle the pay increase request = new Addmoneyrequest (); Tl.handlerequest (request); System.out.println ("========"); Project manager Oberlie resignation request = new Dimissionrequest (); Pm.handlerequest (request);} }
When the responsibility chain pattern is applied, the client code does not have to know in advance which object in the object collection can provide the services it needs. When the client code makes a call request, the request is forwarded along the chain of responsibility until the object providing the service is found. This greatly reduces the degree of coupling between the client code and the object that provides the service.
Java design pattern--chain of Responsibility (chain of responsibility) mode