Java Design Pattern -- Chain of Responsibility (responsible Chain) Pattern
If several objects are organized in a certain hierarchy, for example, as a class responsibility chain, the client code may not have to know which class it will use in advance. In this case, each object in the chain has a method. When the client code calls this method, these objects either execute this method or forward the method call request along this chain.
The responsibility chain mode allows each object to have a chance to determine whether to process the request, so as to avoid coupling between the request sender and the receiver.
The purpose of the responsibility chain mode is to reduce the pressure on callers so that they do not need to know which object can process call requests.
If you encounter the following situations during development, you can consider the application responsibility chain mode:
Multiple objects can process one request, and the object that processes the request is automatically determined at runtime.
You want to submit a request to one of multiple objects without specifying the receiver explicitly.
The object set that can process a request should be dynamically specified
View the class diagram of the application responsibility chain model
1. Define an interface for request processing (optional) to implement a successor chain.
public interface RequestHandle { void handleRequest(Request request);}2. process the requests it is responsible for and can access its successor. If the request can be processed, it will be processed; otherwise, the request will be forwarded to its successor.
Public class HRRequestHandle implements RequestHandle {public void handleRequest (Request request) {if (request instanceof DimissionRequest) {System. out. println ("to resign, personnel approval! ") ;}System. out. println (" request completed ");}}
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 salary, approved by the Project Manager! ") ;}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 ask for leave, approved by the project leader! ") ;}Else {rh. handleRequest (request );}}}3. submit a request to the 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); // The team leader processes the resignation Request request Request = new DimissionRequest (); tl. handleRequest (request); System. out. println ("============"); // team leader handles the salary increase request = new AddMoneyRequest (); tl. handleRequest (request); System. out. println ("========"); // The project manager handles the resignation request = new DimissionRequest (); pm. handleRequest (request );}}After applying the responsibility chain mode, the client code does not need to know in advance which object in the object set can provide the services it needs. After the client code sends a call request, the request is forwarded along the responsibility chain until the object that provides the service is found. This greatly reduces the coupling between client code and the objects that provide services.