Responsibility chain model, as the name implies, is a chain. How does this chain work? It is mainly to be able to handle the same class of requests of the object into a chain, the submitted request along the chain, the chain of the object to determine whether the ability to process the request, if it can be processed, if not passed to the next object on the chain to handle, and so on.
For example, the company needs to be approved for leave, an inappropriate example, if the absence of less than 3 days, supervisor approval, 3-10 days, manager approval, 10-30 days, the general manager for approval; more than 30 days, not approved, etc. This has to be a step by step to judge, if the design mode is not seen, then we can use If...else ... It is solved, but the problem is conceivable, the actual complexity is much more than this example.
Let's use the responsibility chain model to implement this scenario, and first look at the relationship structure between them:
Leader is an abstract class in which there is a way to get the next Leader,director, manager, and Generalmanager to inherit this abstract class, respectively. Information about leave is encapsulated in the leaverequest. Here's how to implement this chain of responsibility model.
The first is the class that encapsulates the leave information:
/** * @Description package basic information on leave * @author Ni SHENGWU * */ Public class leaverequest { PrivateString name;Private intLeavedays;PrivateString reason; Public leaverequest(String name,intLeavedays, String reason) {Super(); This. name = name; This. leavedays = Leavedays; This. Reason = reason; }//Omit Get and Set methods}
And then it's the abstract class that defines leader.
/** * @Description LED abstract class * @author Ni SHENGWU * */ Public Abstract class Leader { protectedString name;protectedLeader Nextleader;//The successor on the chain of responsibility, that is, the object cannot be processed and transferred to the next leader Public Leader(String name) {Super(); This. name = name; }//Set a successor on the responsibility chain Public void Setnextleader(Leader Nextleader) { This. Nextleader = Nextleader; }/** * The business method that processes the core of the request * requires different leaders to inherit the class to implement themselves */ Public Abstract void HandleRequest(Leaverequest request);}
The supervisor, manager and general manager all need to inherit this class and implement this method of processing leave.
/** * @Description Director * @author Ni SHENGWU * */ Public class Director extends Leader { Public Director(String name) {Super(name); }@Override Public void HandleRequest(Leaverequest request) {intDays = Request.getleavedays ();//Get Leave daysString name = Request.getname ();//Get the name of the person to leaveString reason = Request.getreason ();//Get Reasons for leave if(Days <=3) {//If the request within 3 days is met, the director approves directlySystem.out.println ("Staff"+ name +"Leave"+ Days +"Day, Reason:"+ reason); System.out.println ("Director"+ This. Name +"Approval Pass"); }Else{System.out.println ("Too many days off, director"+ This. Name +"Can't handle it.");if( This. nextleader! =NULL) {//Otherwise, if there is a next leader on the chain, let him handle This. nextleader.handlerequest (Request); } } }}
/** * @Description Manager * @author Ni SHENGWU * */ Public class Manager extends Leader { Public Manager(String name) {Super(name); }@Override Public void HandleRequest(Leaverequest request) {intDays = Request.getleavedays ();//Get Leave daysString name = Request.getname ();//Get the name of the person to leaveString reason = Request.getreason ();//Get Reasons for leave if(Days <=Ten) {//If the request within 10 days is met, the manager directly approvesSystem.out.println ("Staff"+ name +"Leave"+ Days +"Day, Reason:"+ reason); System.out.println ("Manager"+ This. Name +"Approval Pass"); }Else{System.out.println ("Too many days to leave, manager"+ This. Name +"Can't handle it.");if( This. nextleader! =NULL) {//Otherwise, if there is a next leader on the chain, let him handle This. nextleader.handlerequest (Request); } } }}
/** * @Description general Manager * @author Ni SHENGWU * */ Public class generalmanager extends Leader { Public Generalmanager(String name) {Super(name); }@Override Public void HandleRequest(Leaverequest request) {intDays = Request.getleavedays ();//Get Leave daysString name = Request.getname ();//Get the name of the person to leaveString reason = Request.getreason ();//Get Reasons for leave if(Days <= -) {//If the requirements within 30 days are met, the general manager directly approvesSystem.out.println ("Staff"+ name +"Leave"+ Days +"Day, Reason:"+ reason); System.out.println ("General Manager"+ This. Name +"Approval Pass"); }Else{System.out.println ("Too many days to leave, general manager"+ This. Name +"Can't handle it.");if( This. nextleader! =NULL) {//Otherwise, if there is a next leader on the chain, let him handle This. nextleader.handlerequest (Request); }Else{System.out.println ("The absence of leave is not successful"); } } }}
Thus, the conditions in which they deal with the request are different, and as long as they are not within their scope, the request is passed on to the next one on the chain. Next write a client-side simulation:
Public class Client { Public Static void Main(string[] args) {Leader Director =NewDirector ("Zhang San"); Leader Manager =NewManager ("John Doe"); Leader Gmanager =NewGeneralmanager ("Harry");//Organizing the relationship of the responsible chain objectDirector.setnextleader (manager); Manager.setnextleader (Gmanager);//start of leave OperationLeaverequest request =NewLeaverequest ("Nu Shen WU", the,"Sleep at Home"); Director.handlerequest (Request); }}
Operation Result:
Too many days to leave, director Zhang San can't handle
Too many days to leave, manager John Doe can't handle
Staff Nu Shen 15 days leave, Reason: sleep at home
General manager Harry approval through
In practice, set this chain can be placed in the configuration file, you can directly modify the configuration file, this chain of responsibility model extensibility is also very good, such as now have a deputy manager, specifically dealing with 10-20 days of leave, then I just add a new deputy manager class, and then configure on the chain of responsibility on the line, Has no effect on other classes.
The responsibility chain model is used in many places, such as our common try...catch ... That is, there can be multiple catch, one by one, and then the filter inside the servlet is a chain of responsibility, such as the Interceptor in Struts2, is also the use of the chain of responsibility mode.
-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15
Java design mode responsibility chain (chain of resposibility) mode