public class Client { public static void main(String args[]) { Leader objDirector,objManager,objGeneralManager,objViceGeneralManager; objDirector=new Director("王明"); objManager=new Manager("赵强"); objGeneralManager=new GeneralManager("李波"); objViceGeneralManager=new ViceGeneralManager("肖红"); objDirector.setSuccessor(objManager); objManager.setSuccessor(objViceGeneralManager); objViceGeneralManager.setSuccessor(objGeneralManager); LeaveRequest lr1=new LeaveRequest("张三",5000); objDirector.handleRequest(lr1); LeaveRequest lr2=new LeaveRequest("李四",20000); objDirector.handleRequest(lr2); LeaveRequest lr3=new LeaveRequest("王五",80000); objDirector.handleRequest(lr3); LeaveRequest lr4=new LeaveRequest("赵六",140000); objDirector.handleRequest(lr4); } }
public class Director extends Leader { public Director(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<10000) { System.out.println("主任" + name + "审批" + request.getLeaveName() + "的购物清单,金额为" + request.getLeaveDays() + "元。"); } else { if(this.successor!=null) { this.successor.handleRequest(request); } } } }
public class GeneralManager extends Leader { public GeneralManager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<200000) { System.out.println("总经理" + name + "审批" + request.getLeaveName() + "的购物清单,金额为" + request.getLeaveDays() + "元。"); } else { System.out.println("总经理没有权限批准"); } } }
public abstract class Leader { protected String name; protected Leader successor; public Leader(String name) { this.name=name; } public void setSuccessor(Leader successor) { this.successor=successor; } public abstract void handleRequest(LeaveRequest request); }
public class LeaveRequest { private String leaveName; private int leaveDays; public LeaveRequest(String leaveName,int leaveDays) { this.leaveName=leaveName; this.leaveDays=leaveDays; } public void setLeaveName(String leaveName) { this.leaveName = leaveName; } public void setLeaveDays(int leaveDays) { this.leaveDays = leaveDays; } public String getLeaveName() { return (this.leaveName); } public int getLeaveDays() { return (this.leaveDays); } }
public class Manager extends Leader { public Manager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<50000) { System.out.println("部门经理" + name + "审批" + request.getLeaveName() + "的购物清单,金额为" + request.getLeaveDays() + "元。"); } else { if(this.successor!=null) { this.successor.handleRequest(request); } } } }
public class ViceGeneralManager extends Leader { public ViceGeneralManager(String name) { super(name); } public void handleRequest(LeaveRequest request) { if(request.getLeaveDays()<100000) { System.out.println("副总经理" + name + "审批" + request.getLeaveName() + "的购物清单,金额为" + request.getLeaveDays() + "元。"); } else { if(this.successor!=null) { this.successor.handleRequest(request); } } } }