赞
踩
/** * 请假申请 */ public class Leave { // 请假人 private String name; // 请假天数 private int days; public Leave(String name, int days) { this.name = name; this.days = days; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDays() { return days; } public void setDays(int days) { this.days = days; } @Override public String toString() { return this.getName()+"请假"+this.getDays()+"天"; } }
/** * 请假审批抽象类 */ public abstract class LeaveHandler { // 需要总经理批准的请假天数 protected int MANAGER_LEAVE_REQUEST_DAYS=3; // 主管名字 private String name; // 审批意见 private boolean approved; public LeaveHandler(String name) { this.name = name; } // 下一节点 protected LeaveHandler next; // 处理请假 public abstract void handle(Leave request); // 设置下一节点 protected LeaveHandler setNextHandler(LeaveHandler handler){ this.next = handler; return next; } public LeaveHandler getNext() { return next; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getManagerLeaveRequestDays() { return MANAGER_LEAVE_REQUEST_DAYS; } public void setApproved(boolean approved) { this.approved = approved; } public boolean getApproved() { return approved; } }
/** * 部门主管处理请假 */ public class DepartmentLeaveHandler extends LeaveHandler{ public DepartmentLeaveHandler(String name) { super(name); } @Override public void handle(Leave request) { if (this.getApproved()){ System.out.println(this.getName()+"审批通过:"+request.toString()); if (this.next!=null){ this.next.handle(request); } }else{ System.out.println(this.getName()+"审批拒绝:"+request.toString()); } } }
/** * 分公司领导处理请假 */ public class CompanyLeaveHandler extends LeaveHandler { public CompanyLeaveHandler(String name) { super(name); } @Override public void handle(Leave request ) { if (this.getApproved()) { System.out.println(this.getName() + "审批通过:"+request.toString()); // 如果超过三天,需要总经理审批 if (this.next != null && request.getDays() > this.getManagerLeaveRequestDays()) { this.next.handle(request); } } else { System.out.println(this.getName() + "审批拒绝:"+request.toString()); } } }
/** * 总经理请假处理 */ public class ManagerLeaveHandler extends LeaveHandler { public ManagerLeaveHandler(String name) { super(name); } @Override public void handle(Leave request ) { if (this.getApproved()){ System.out.println(this.getName()+"审批通过:"+request.toString()); if (this.next!=null){ this.next.handle(request); } }else{ System.out.println(this.getName()+"审批拒绝:"+request.toString()); } } }
/** * 请假流程测试 */ public class TestLeave { public static void main(String[] args) { // 形成责任链 DepartmentLeaveHandler departmentLeaveHandler = new DepartmentLeaveHandler("部门主管"); CompanyLeaveHandler companyLeaveHandler = new CompanyLeaveHandler("分公司领导"); ManagerLeaveHandler managerLeaveHandler = new ManagerLeaveHandler("总经理"); departmentLeaveHandler.setNextHandler(companyLeaveHandler).setNextHandler(managerLeaveHandler); // 张三请假4天 Leave leave1 = new Leave("张三",4); // 部门主管同意 departmentLeaveHandler.setApproved(true); // 分公司领导同意 companyLeaveHandler.setApproved(true); // 总经理审批拒绝 managerLeaveHandler.setApproved(false); departmentLeaveHandler.handle(leave1); } }
/** * 请假流程测试 */ public class TestLeave { public static void main(String[] args) { // 形成责任链 DepartmentLeaveHandler departmentLeaveHandler = new DepartmentLeaveHandler("部门主管"); CompanyLeaveHandler companyLeaveHandler = new CompanyLeaveHandler("分公司领导"); ManagerLeaveHandler managerLeaveHandler = new ManagerLeaveHandler("总经理"); departmentLeaveHandler.setNextHandler(companyLeaveHandler).setNextHandler(managerLeaveHandler); // 张三请假4天 Leave leave1 = new Leave("李四",2); departmentLeaveHandler.setApproved(true); companyLeaveHandler.setApproved(true); departmentLeaveHandler.handle(leave1); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。