/** * Leave section * Package leave information. */package com.bjsxt.cn.chainOfResponsibility;
public class Leaverequest {private string empname; private int leavedays; private string reasons; Public Leaverequest (string empname, int leavedays, string reasons) {super (); This.empname = EmpName; This.leavedays = Leavedays; This.reasons = reasons; }
Public String Getempname () {return empname;}
public void Setempname (String empname) {this.empname = EmpName;}
public int getleavedays () {return leavedays;}
public void setleavedays (int leavedays) {this.leavedays = leavedays;}
Public String getreasons () {return reasons;}
public void Setreasons (String reasons) {this.reasons = reasons;} @Override public String toString () {return "Employee:" + EmpName + "Leave, Days:" + leavedays + ", Reason for leave:" + reasons; }}
/** * Abstract class leader, in the process of leave, the approval staff * Director, Manager, General manager, abstracted into a class, they * have the same function, approval. HandleRequest * April 9, 2015 14:40:49 * */package com.bjsxt.cn.chainOfResponsibility;
Public abstract class Leader {protected String name; protected Leader Nextleader; Public Leader (String name) {super (); THIS.name = name; }
public void Setnextleader (Leader nextleader) {this.nextleader = Nextleader;} public abstract void HandleRequest (Leaverequest request); Public abstract String Check ();}
/** * Director class: * if the number of days of absence is less than 3 days, director approval * April 9, 2015 14:43:36 */package com.bjsxt.cn.chainOfResponsibility;
public class Director extends Leader {
Public Director (String name) {super (name);}
@Override public void HandleRequest (Leaverequest request) {if (Request.getleavedays () < 3) {SYSTEM.OUT.PRINTLN ("employee Name: "+ request.getempname () +" Leave, Days: "+ request.getleavedays () +" Reason for Leave "+request.getreasons ()); System.out.println (check ()); } else {if (this.nextleader! = null) {this.nextLeader.handleRequest (request); } } }
@Override public String Check () {return "Director:" + name + "approval passed!"; } }
/** * Time: April 9, 2015 14:48:13 * Manager class: If the number of leave is greater than or equal to 3 days, less than 10 days, manager approval */package com.bjsxt.cn.chainOfResponsibility;
public class Manager extends Leader {
Public Manager (String name) {super (name);} @Override public void HandleRequest (Leaverequest request) {if (Request.getleavedays () <10) {System.out.println (req Uest.tostring ()); System.out.println (check ()); } else {if (this.nextleader! = null) {this.nextLeader.handleRequest (request); } } }
@Override public String Check () {return "Manager:" + name + "approval Pass";}
}
/** * General Manager: if greater than or equal to 10 days, less than 30 days, general manager approval * Time: April 9, 2015 14:53:41 */package com.bjsxt.cn.chainOfResponsibility;
public class Generalmanager extends Leader {
Public Generalmanager (String name) {super (name);} @Override public void HandleRequest (Leaverequest request) {if (Request.getleavedays () <) {System.out.println (re Quest.tostring ()); System.out.println (check ()); } else {System.out.println (request.tostring () + "does not want to resign, does not approve"); } }
@Override public String Check () {return "General Manager:" + name + "Audit Pass";}
}
/** * April 9, 2015 14:59:50 * Test procedure: Test the use of the responsibility chain model * We use the responsibility chain model rather than the very cumbersome complex ifelse structure, is for the convenience of expansion. * For example, if we want to join a deputy general manager to handle the 10-20-day slips Audit * We only need to add the new class Vicegeneralmanager and create a new responsibility chain in the client program. * This is in line with the OCP principle, the open and closed principle. */package com.bjsxt.cn.chainOfResponsibility;
public class Client {public static void main (string[] args) {leaverequest L1 = new Leaverequest ("Jay", 1, "travel"); Leaverequest L2 = new Leaverequest ("Jane Zhang", 8, "Love"); Leaverequest L3 = new Leaverequest ("Charlotte", 15, "lovelorn"); Leaverequest L4 = new Leaverequest ("Bach", 37, "with children at Home"); Leader LL1 = new Director ("Zhang"); Leader ll2 = new manager ("manager Li"); Leader ll3 = new Generalmanager ("Wang General manager"); Create the responsibility chain Ll1.setnextleader (LL2); Ll2.setnextleader (LL3); Ll1.handlerequest (L1); Ll1.handlerequest (L2); Ll1.handlerequest (L3); Ll1.handlerequest (L4); }}/* Employee Name: Jay Leave, Days: 1 reasons for leave for Travel director: Zhang Director approval through! Employees: Jane Zhang Leave, days: 8, reason for leave: Love Manager: Manager Li approval through employees: Charlotte leave, Days: 15, reasons for leave: lovelorn General Manager: Wang General manager audit through employees: Bach leave , Number of days: 37, reasons to leave: At home with the children do not want to resign, do not approve
* * */
/** * joined the deputy general manager to handle 10 days to 20 days slips audit * April 9, 2015 15:12:46 */package com.bjsxt.cn.chainOfResponsibility;
public class Vicegeneralmanager extends leader{
Public Vicegeneralmanager (String name) {super (name);}
@Override public void HandleRequest (Leaverequest request) {if (Request.getleavedays () <) {System.out.println (re Quest.tostring ()); System.out.println (check ()); } else {if (this.nextleader! = null) {this.nextLeader.handleRequest (request); } } }
@Override public String Check () {return "Deputy General Manager:" + name + "Audit Pass";}
}
/** * Add Vicegeneralmanager class and then tweak it to add a new chain of responsibility after joining the class. */package com.bjsxt.cn.chainOfResponsibility;
public class Client2 { public static void main (string[] args) { leaverequest L1 = new Leaverequest ("Jay", 1 , "Travel"); leaverequest L2 = new Leaverequest ("Jane Zhang", 8, "Love"); leaverequest L3 = new Leaverequest ("Charlotte Pedicle ", 15," lovelorn "); leaverequest L4 = new Leaverequest (" Bach ", 37," with children at Home "); leader ll1 = new Director ("Zhang"), leader ll2 = new manager ("manager Li"), leader ll22 = new Vicegeneralmanager ("Vice general manager of Wang"); leader ll3 = new Generalmanager ("Wang General manager") //create a responsibility chain ll1.setnextleader ( LL2); ll2.setnextleader (ll22); ll22.setnextleader (LL3); Ll1.handlerequest (L1); ll1.handlerequest (L2); ll1.handlerequest (L3); Ll1.handlerequest (L4); }}/* * Employee Name: Jay Leave, Days: 1 reasons for leave for Travel director: Zhang Director approval through! Employees: Jane Zhang Leave, days: 8, Reason for leave: Love Manager: Manager Li approval through employees: Charlotte leave, Days: 15, reason for leave: lovelorn Vice General Manager: Wang deputy general manager audit through employees: Bach leave, Days: 37, leave reason: At home with children do not want to resign, do not approve
* * */
Implementation of the responsibility chain mode of GOF23 design mode (chain of responsibility)