package com.bdqn.bs; /** * 封装请假的基本信息 * @author OU * */ public class LeaveRequest { private String empName; private int leaveDays; private String reason; public LeaveRequest(String empName, int leaveDays, String reason) { this.empName = empName; this.leaveDays = leaveDays; this.reason = reason; } public LeaveRequest() { } 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 getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } }
package com.bdqn.bs; /** * 抽象类 * @author OU * */ public abstract class Leader { protected String name; protected Leader nextLeader;//责任链上的后连对象 public Leader(String name) { this.name = name; } //设定责任链上的后继对象 public void setNextLeader(Leader nextLeader) { this.nextLeader = nextLeader; } //处理请求的核心的业务方法 public abstract void handleRequest(LeaveRequest request); }
package com.bdqn.bs; /** * 主管 * @author OU * */ public class Director extends Leader{ public Director(String name) { super(name); } public void handleRequest(LeaveRequest request) { if ( request.getLeaveDays() < 3) { System.out.println("员工:"+request.getEmpName()+"请假,天数"+request.getLeaveDays()+"理由"+request.getReason()); System.out.println("主任:"+this.name+"审批通过"); } else { if (this.nextLeader!=null) { this.nextLeader.handleRequest(request); } } } }
package com.bdqn.bs; /** * 经理 * @author OU * */ public class Manager extends Leader{ public Manager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if ( request.getLeaveDays() < 10) { System.out.println("员工:"+request.getEmpName()+"请假,天数"+request.getLeaveDays()+"理由"+request.getReason()); System.out.println("经理:"+this.name+"审批通过"); } else { if (this.nextLeader!=null) { this.nextLeader.handleRequest(request); } } } }
package com.bdqn.bs; public class GeneralManager extends Leader{ public GeneralManager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if ( request.getLeaveDays() < 30) { System.out.println("员工:"+request.getEmpName()+"请假,天数"+request.getLeaveDays()+"理由"+request.getReason()); System.out.println("总经理:"+this.name+"审批通过"); } else { System.out.println("莫非"+request.getEmpName()+"想辞职"); }{ } } }
package com.bdqn.bs; public class Client { public static void main(String[] args) { Leader a=new Director("张三"); Leader b=new Manager("李四"); Leader c=new GeneralManager("王五"); //住址责任链对象的关系 a.setNextLeader(b); b.setNextLeader(c); //请假三天 LeaveRequest res=new LeaveRequest("欧长路",4, "不舒服"); a.handleRequest(res); } }