标签:
来源:http://www.bjsxt.com/
一、【GOF23设计模式】_责任链模式、公文审批、供应链系统的采购审批、异常链、过滤器和拦截器调用过程
1 package com.test.chainOfResp; 2 /** 3 * 封装请假的基本信息 4 */ 5 public class LeaveRequest { 6 private String empName; 7 private int leaveDays; 8 private String reason; 9 10 public LeaveRequest(String empName, int leaveDays, String reason) { 11 super(); 12 this.empName = empName; 13 this.leaveDays = leaveDays; 14 this.reason = reason; 15 } 16 17 public String getEmpName() { 18 return empName; 19 } 20 21 public void setEmpName(String empName) { 22 this.empName = empName; 23 } 24 25 public int getLeaveDays() { 26 return leaveDays; 27 } 28 29 public void setLeaveDays(int leaveDays) { 30 this.leaveDays = leaveDays; 31 } 32 33 public String getReason() { 34 return reason; 35 } 36 37 public void setReason(String reason) { 38 this.reason = reason; 39 } 40 }
1 package com.test.chainOfResp; 2 /** 3 * 抽象类 4 */ 5 public abstract class Leader { 6 protected String name; 7 protected Leader nextLeader;//责任链上的后继对象 8 9 public Leader(String name) { 10 super(); 11 this.name = name; 12 } 13 //设定责任链上的后继对象 14 public void setNextLeader(Leader nextLeader) { 15 this.nextLeader = nextLeader; 16 } 17 18 /** 19 * 处理请求的核心的业务方法 20 * @param leaveRequest 21 */ 22 public abstract void handleRequest(LeaveRequest leaveRequest); 23 }
1 package com.test.chainOfResp; 2 /** 3 * 主任 4 */ 5 public class Director extends Leader{ 6 7 public Director(String name) { 8 super(name); 9 } 10 11 @Override 12 public void handleRequest(LeaveRequest leaveRequest) { 13 if(leaveRequest.getLeaveDays() < 3){ 14 System.out.println("员工:"+leaveRequest.getEmpName()+"请假,天数:"+leaveRequest.getLeaveDays()+",理由:"+leaveRequest.getReason()); 15 System.out.println("主任:"+this.name+",审批通过!"); 16 }else{ 17 if(this.nextLeader != null){ 18 this.nextLeader.handleRequest(leaveRequest); 19 } 20 } 21 } 22 }
1 package com.test.chainOfResp; 2 /** 3 * 经理 4 */ 5 public class Manager extends Leader{ 6 7 public Manager(String name) { 8 super(name); 9 } 10 11 @Override 12 public void handleRequest(LeaveRequest leaveRequest) { 13 if(leaveRequest.getLeaveDays() < 10){ 14 System.out.println("员工:"+leaveRequest.getEmpName()+"请假,天数:"+leaveRequest.getLeaveDays()+",理由:"+leaveRequest.getReason()); 15 System.out.println("经理:"+this.name+",审批通过!"); 16 }else{ 17 if(this.nextLeader != null){ 18 this.nextLeader.handleRequest(leaveRequest); 19 } 20 } 21 } 22 }
1 package com.test.chainOfResp; 2 /** 3 * 总经理 4 */ 5 public class GeneralManager extends Leader{ 6 7 public GeneralManager(String name) { 8 super(name); 9 } 10 11 @Override 12 public void handleRequest(LeaveRequest leaveRequest) { 13 if(leaveRequest.getLeaveDays() < 30){ 14 System.out.println("员工:"+leaveRequest.getEmpName()+"请假,天数:"+leaveRequest.getLeaveDays()+",理由:"+leaveRequest.getReason()); 15 System.out.println("总经理:"+this.name+",审批通过!"); 16 }else{ 17 System.out.println("莫非"+leaveRequest.getEmpName()+"想辞职,居然请假"+leaveRequest.getLeaveDays()+"天!"); 18 } 19 } 20 }
1 package com.test.chainOfResp; 2 /** 3 * 副总经理 4 */ 5 public class ViceGeneralManager extends Leader{ 6 7 public ViceGeneralManager(String name) { 8 super(name); 9 } 10 11 @Override 12 public void handleRequest(LeaveRequest leaveRequest) { 13 if(leaveRequest.getLeaveDays() < 20){ 14 System.out.println("员工:"+leaveRequest.getEmpName()+"请假,天数:"+leaveRequest.getLeaveDays()+",理由:"+leaveRequest.getReason()); 15 System.out.println("副总经理:"+this.name+",审批通过!"); 16 }else{ 17 if(this.nextLeader != null){ 18 this.nextLeader.handleRequest(leaveRequest); 19 } 20 } 21 } 22 }
1 package com.test.chainOfResp; 2 3 public class Client { 4 public static void main(String[] args) { 5 Leader a = new Director("张三"); 6 Leader b = new Manager("李四"); 7 Leader b2 = new ViceGeneralManager("李小四");//新增逻辑,副总经理 8 Leader c = new GeneralManager("王五"); 9 10 //组织责任链对象的关系 11 a.setNextLeader(b); 12 b.setNextLeader(b2);//新增关系 13 b2.setNextLeader(c); 14 15 //开始请假操作 16 LeaveRequest leaveRequest = new LeaveRequest("Tom", 15, "回英国老家探亲!"); 17 a.handleRequest(leaveRequest); 18 19 } 20 }
控制台输出: 员工:Tom请假,天数:15,理由:回英国老家探亲! 副总经理:李小四,审批通过!
标签:
原文地址:http://www.cnblogs.com/erbing/p/5802581.html