设计模式之责任链模式
责任链模式
将能够处理一类请求的对象连成一条链,所提交的请求沿着链传递,链上的对象逐个判断是否有能力处理该请求,如果能则处理,如果不能则传递给链上的下一个对象。
使用场景
(1)在公司OA系统请假审批流程
如果请假小于3天只需要项目经理批复就行;
如果请假大于等于3天,小于7天需要人事经理批复了;
如果请假大于等于7天,小于15天需要总经理批复了;
如果申请请假大于等于15天,决绝批复......
(2)公司报销审批流程
金额小于1000,经理审查;
金额超过1000,总经理审查......
责任链设计模式包含的角色
Handler:抽象处理者(定义一个处理请求的接口,接口可以定义出一个方法,以设定和返回对下家的引用。通常由一个抽象类或接口实现)
ConcreteHandler:具体处理者(具体请求者在接受到请求后,如果满足条件则自己处理请求,否则将请求传到下家。具体处理者持有下家对象的引用)
Client:客户端(调用者)
模式结构如下:
责任链模式测试请假流程
(1)封装请假人员实体类
/** * LeaveRequest 封装请假实体类 * @author yangkj*/ public class LeaveRequest { /** * 员工姓名 */ private String employee; /** * 请假天数 */ private int leaveDay; /** * 请假原因 */ private String reason;
......(省略get/set及构造函数)
}
(2)抽象处理者
/** * Leader 领导人 * @author yangkj*/ 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; } /** * 处理相应的请求 * * @param request */ public abstract void dealLeaveRequest(LeaveRequest request); }
(3)具体处理者
/** * Manager 经理 * @author yangkj*/ public class Manager extends Leader { public Manager(String name) { super(name); } @Override public void dealLeaveRequest(LeaveRequest request) { if (request.getLeaveDay() < 3) { System.out.println("经理:" + name + " 审批了 " + request.getEmployee() + "请假" + request.getLeaveDay() + "天的请求,请假原因:" + request.getReason()); } else { if (this.nextLeader != null) { this.nextLeader.dealLeaveRequest(request); } } } }
/** * HrManager 人事经理 * @author yangkj*/ public class HrManager extends Leader { public HrManager(String name) { super(name); } @Override public void dealLeaveRequest(LeaveRequest request) { if (3 <= request.getLeaveDay() && request.getLeaveDay() < 7) { System.out.println("人事经理:" + name + " 审批了 " + request.getEmployee() + "请假" + request.getLeaveDay() + "天的请求,请假原因:" + request.getReason()); } else { if (this.nextLeader != null) { this.nextLeader.dealLeaveRequest(request); } } } }
/** * GeneralManager 总经理 * @author yangkj */ public class GeneralManager extends Leader { public GeneralManager(String name) { super(name); } @Override public void dealLeaveRequest(LeaveRequest request) { if (7 <= request.getLeaveDay() && request.getLeaveDay() <= 15) { System.out.println("总经理:" + name + " 审批了 " + request.getEmployee() + "请假" + request.getLeaveDay() + "天的请求,请假原因:" + request.getReason()); } else { System.out.println( "总经理:" + name + " 拒绝了 " + request.getEmployee() + "请假" + request.getLeaveDay() + "天的请求,请假不能超过15天"); } } }
(4)测试代码
/** * Client 测试代码 * @author yangkj */ public class Client { public static void main(String[] args) { Leader manager = new Manager("张三"); Leader hrManager = new HrManager("李四"); Leader generalManager = new GeneralManager("王麻子"); //组织责任链对象的关系 manager.setNextLeader(hrManager); hrManager.setNextLeader(generalManager); /**********************上面的内容可以坐在配置文件中****************************/ //请假 LeaveRequest request = new LeaveRequest("parry", 20, "回家相亲!"); manager.dealLeaveRequest(request); } }