一个请求有多个对象可以处理,但每个对象的处理条件或权限不同。
定义:
为了避免请求发送者与多个请求处理者耦合在一起(使用 if 选择语句处理请求),将所有请求的处理者通过前一对象记住其下一个对象的引用(链表)而连成一条链;当有请求发生时,可将请求沿着这条链传递,直到有对象处理它为止。
结构:
请求对象:需要被处理的信息。
抽象处理类:待实现的处理方法,以及指向下一个处理类的引用。
处理类:具体的处理类
例如:
请假条:
public class LeaveRequest { private String name;//请加人姓名 private int leaveDays;//请假几天 private String reason;//请假事由 public LeaveRequest(String name, int leaveDays, String reason) { this.name = name; this.leaveDays = leaveDays; this.reason = reason; } public String getName() { return name; } public int getLeaveDays() { return leaveDays; } public String getReason() { return reason; } }
审批人员抽象类,具备待实现的审批假条的方法。
public abstract class Leaders { protected String name;//审批者 protected Leaders nextLeader;//下一级审批对象 public Leaders(String name){ this.name = name; } //设置下一级审批对象 public void setNextLeader(Leaders nextLeader) { this.nextLeader = nextLeader; } /** * 处理请求的核心业务方法 */ public abstract void handlerRequest(LeaveRequest request); /** * 显示审批信息 */ protected void dispayDatas(LeaveRequest request){ System.out.println("请假人:"+request.getName()); System.out.println("请假天数:"+request.getLeaveDays()); System.out.println("事由:"+request.getReason()); System.out.println("审批人:"+this.name); System.out.println("------------------------------"); } }
具体审批人员1:主任可批三天以内的假。
public class Director extends Leaders{ //构造器 public Director(String name) { super(name); } //可批三天以内假条 @Override public void handlerRequest(LeaveRequest request) { if(request.getLeaveDays()<3){ this.dispayDatas(request);//显示审批内容 }else { this.nextLeader.handlerRequest(request);//交给上级处理 } } }
具体审批人员2:经理可批15天以内的假。
public class Manage extends Leaders{ public Manage(String name) { super(name); } //可批十天以内 @Override public void handlerRequest(LeaveRequest request) { if(request.getLeaveDays()<10){ this.dispayDatas(request);//显示审批内容 }else { this.nextLeader.handlerRequest(request);//交给上级处理 } } }
具体审批人员2:经理可批30天以内的假。
public class GeneralManage extends Leaders{ public GeneralManage(String name) { super(name); } @Override public void handlerRequest(LeaveRequest request) { if(request.getLeaveDays()<30){ this.dispayDatas(request);//显示审批内容 }else { System.out.println(request.getName()+"请假:"+request.getLeaveDays()+"天"); System.out.println(this.name+"批示:建议申请离职。"); } } }
测试:
public static void main(String[] args) { //创建处理者 Leaders leaders1 = new Director("好主任"); Leaders leaders2 = new Manage("流经理"); Leaders leaders3 = new GeneralManage("呱总经理"); //组装责任链对象的关系 leaders1.setNextLeader(leaders2); leaders2.setNextLeader(leaders3); //创建请求 LeaveRequest request1 = new LeaveRequest("猪王",2,"回去睡觉打呼噜"); LeaveRequest request2 = new LeaveRequest("佩奇",9,"去做电吹风机器"); LeaveRequest request3 = new LeaveRequest("虹猫",23,"准备去仗剑走天涯"); LeaveRequest request4 = new LeaveRequest("大奔",45,"练习奔雷剑法"); //处理请求 leaders1.handlerRequest(request1); leaders1.handlerRequest(request2); leaders1.handlerRequest(request3); leaders1.handlerRequest(request4); }
测试结果:
标签:name,chain,request,Leaders,责任,responsibility,LeaveRequest,public,String From: https://www.cnblogs.com/lurenjia-bky/p/16926639.html