Responsibility Chain Pattern of JAVA design Pattern [Chain of Responsibility Pattern], javapattern
I. Overview
To avoid coupling between request senders and receivers, multiple objects may receive requests, connect these objects into a chain, and pass requests along the chain, until an object processes it. The responsibility chain mode is an object behavior mode.
The core is to introduce an abstract processor class.
Ii. Applicable scenarios
Request chain processing. Multiple objects can process the same request, but the system determines the reason for the specific object.
For example:
Iii. UML class diagram
4. Participants
1. Handler (Abstract Handler): it defines an interface for processing requests. It is generally designed as an abstract class. Because different processors process requests differently, therefore, the abstract request processing method is defined. Because each processor is still a processor, the abstract processor defines an object of the abstract Processor type (such as nextHandler in the structure diagram) as a reference to the next processor. With this reference, the processor can join a chain.
2. ConcreteHandler (Specific handler): It is a subclass of the abstract handler and can process user requests. In the specific handler class, it implements the abstract request processing method defined by the abstract handler, before processing a request, you need to determine whether the request has the corresponding processing permission. If the request can be processed, it will be processed; otherwise, the request will be forwarded to the successor; the specific handler can access the next object in the chain for request forwarding.
Note: The responsibility chain mode does not create a responsibility chain. The creation of a responsibility chain must be completed by other parts of the system. Generally, a responsibility chain is created on the client that uses this responsibility chain.
V. Use Case Learning
1. Abstract Handler class: Handler. java
/*** Abstract Handler class * @ author lvzb.software@qq.com ***/public abstract class Handler {protected Handler nextHandler; public void setNextHandler (Handler nextHandler) {this. nextHandler = nextHandler;} public abstract void handleRequest (int request );}
2. Specific handler Class A: department head ConcreteHandlerA. java
/*** Specific responsibilities Handler A: team lead role in the case * @ author lvzb.software@qq.com **/public class ConcreteHandlerA extends Handler {@ Overridepublic void handleRequest (int leaveDay) {if (leaveDay <= 1) {// The request System must meet the processing conditions. out. println ("leave days less than 2 days approved by the team lead");} else {nextHandler. handleRequest (leaveDay );}}}
3. Specific handler Class B: department manager ConcreteHandlerB. java
/*** Specific responsibilities Handler B: department manager role in the case * @ author lvzb.software@qq.com **/public class ConcreteHandlerB extends Handler {@ Overridepublic void handleRequest (int leaveDay) {if (1 <leaveDay & leaveDay <= 5) {// The request System meets the processing conditions. out. println ("the number of days for leave is greater than 1 day and less than or equal to 5 days approved by the department manager");} else {nextHandler. handleRequest (leaveDay );}}}
4. Specific handler Class C: ConcreteHandlerC. java, General Manager
/*** Specific responsibilities Handler C: General Manager role in the case * @ author lvzb.software@qq.com **/public class ConcreteHandlerC extends Handler {@ Overridepublic void handleRequest (int leaveDay) {if (leaveDay> 5) {System. out. println. The general manager has the greatest responsibilities. Is there anything else that he has no permission to handle? ");}}}
5. Client class: Client. java
/*** Client class <br/> * responsible for creating the responsibility chain and sending requests <br/> * after the responsibility chain (transfer order of duties/request processing order) is established, the client is only responsible for sending requests, * The transfer of specific requests on the responsibility chain and which object on the chain is finally processed by the Client does not care * @ author lvzb.software@qq.com */public class Client {public static void main (String [] args) {Handler handlerA, handlerB, handlerC; handlerA = new ConcreteHandlerA (); handlerB = new ConcreteHandlerB (); handlerC = new ConcreteHandlerC (); // create the responsibility chain handlerA --> handlerB --> handlerChandlerA. setNextHandler (handlerB); handlerB. setNextHandler (handlerC); // send a handlerA request for leave. handleRequest (1); // send the leave request 2 handlerA. handleRequest (7); // send the leave request 2 handlerA. handleRequest (3 );}}
6. Running result:
If the number of days for leave is less than two days, the department leader will approve the number of days for leave. If the number of days for leave is greater than five days, the general manager will perform the Examination and Approval in person. The general manager has the greatest responsibilities. Is there anything else that he has no permission to handle? Leave days greater than 1 day and less than or equal to 5 days approved by the department manager