I. Overview
avoid the request sender to be coupled with the receiver, so that multiple objects are likely to receive requests, connect those objects into a chain, and pass the request along this chain until an object has processed it. The responsibility chain pattern is an object-based behavioral pattern .
The core is the introduction of an abstract processor class
Second, the application scenario
for chained processing of requests , multiple objects can handle the same request, but the specific object to handle is determined by the runtime system based on the condition.
such as leave business scenario:
Third, UML class diagram
Iv. participants
1, Handler (abstract processor): It defines a processing request interface, generally designed as abstract class, because different specific processor processing requests differently, so in which the abstract request processing method is defined. Because each processor's next is a processor, an object of an abstract processor type, such as Nexthandler in a structure diagram, is defined in the abstract processor as a reference to the next. With this reference, the processor can be linked into a chain.
2, Concretehandler (specific processor): It is the subclass of the abstract processor, can handle the user request, in the specific processor class to implement the abstract processing of the abstract request processing method, before processing the request needs to be judged, to see if there are appropriate processing permissions, If the request can be processed, the request is forwarded to the successor, and the next object in the chain can be accessed by the specific processor in order to forward the request.
Note: The responsibility chain model does not create a chain of responsibilities, and the creation of a responsibility chain must be done by other parts of the system, typically creating a chain of responsibility in the client that uses that chain of responsibility.
V. Use case Learning
1. Abstract Processor class: Handler.java
/** * Abstract Processor class * @author [email protected] * */public abstract class Handler {protected Handler nexthandler;public void Setnexthandler (Handler nexthandler) {this.nexthandler = Nexthandler;} public abstract void HandleRequest (int request);}
2, the specific processor Class A: Department leaderConcretehandlera.java
/** * Specific Responsibilities processor A: role of department leader in the case * @author [email protected] * */public class Concretehandlera extends Handler {@Overridepub LIC void handlerequest (int leaveday) {if (Leaveday <= 1) {//satisfies processing condition processing request SYSTEM.OUT.PRINTLN ("The number of days of absence is less than 2 days approved by Department leader ");} else {nexthandler.handlerequest (leaveday);}}}
3.Specific Processor Class B: Department managerConcretehandlerb.java
/** * Specific Responsibilities Processor B: the role of the department manager in the case * @author [email protected] * */public class Concretehandlerb extends Handler {@Overridepub LIC void handlerequest (int leaveday) {if (1 < leaveday && Leaveday <= 5) {//Meet processing Conditions processing request SYSTEM.OUT.PRINTL N ("The number of days to leave is greater than 1 days and less than or 5 days are approved by the department manager");} else {nexthandler.handlerequest (leaveday);}}}
4.specific Processor Class C: General managerConcretehandlerc.java
/** * Specific Responsibilities processor C: The general manager role in the case * @author [email protected] * */public class Concretehandlerc extends Handler {@Overridepubl IC void handlerequest (int leaveday) {if (Leaveday > 5) {System.out.println ("when the number of days of absence is greater than 5 days) is approved by the general manager in person. The responsibility of the general manager is already the biggest, and he has no authority to deal with it? ");}}}
5. Client class: Client.java
/** * Client Class <br/> * Responsible for creating responsibility chains and sending requests <br/> * when the responsibility chain (the order of responsibility delivery/request processing) is built, the client is only responsible for sending the request out, * and the specific request is passed on the responsibility chain and ultimately by which object on the chain is processed not by the client * @author [email protected] * */public class Client {public static void main (Stri Ng[] args) {Handler Handlera,handlerb,handlerc;handlera = new Concretehandlera (); handlerb = new Concretehandlerb (); Handlerc = new Concretehandlerc ();//Create responsibility chain handlera--> handlerb--> Handlerchandlera.setnexthandler ( Handlerb); Handlerb.setnexthandler (HANDLERC);//Send leave request one handlera.handlerequest (1);//Send leave request two handlera.handlerequest (7);//Send leave request two handlera.handlerequest (3); }}
6. Operation Result:
The number of days to leave is less than 2 days from the department leader approval when the number of days of absence is greater than 5 days by the general manager personally fencing approval. The responsibility of the general manager is already the biggest, and he has no authority to deal with it? Leave days greater than 1 days and less than 5 days approved by Department manager
The responsibility chain mode of Java design mode "Chain of Responsibility pattern"