设计模式之GOF23责任链模式
阅读原文时间:2023年07月08日阅读:1

责任链模式chain of responsibility

将能够处理同一类请求的对象连成一条链,所提交的请求依次在链上传递,直到传递至有能力处理该请求的对象,不能则传给链上下一个

场景:

-打牌时

-奖学金审批

-请假审批

核心:是在客户端中才成链,也可以通过配置文件或者存在数组中加载

开发中常见场景:

-try catch异常链

-javascript语言中,事件的冒泡和捕获机制 Java语言中,事件处理采用观察者模式

-Servlet开发中,过滤器的链式处理

-Struts2中,拦截器的调用也是典型的链式处理

例子:工作中请假

0-3主任审批  3-10经理审批  10-30总经理审批

/**
 * 请假条
 * @author 小帆敲代码
 *
 */

public class LeaveRequest {
   private String empName;
   private int leaveDays;
   private String reason;
   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;
   }
   public LeaveRequest(String empName, int leaveDays, String reason) {
    this.empName = empName;
    this.leaveDays = leaveDays;
    this.reason = reason;
   }
}

public abstract class Leader {
  protected String leaderName;
  protected Leader nextLeader;//责任链中的后继对象
  public Leader(String leaderName) {
   this.leaderName = leaderName;
  }
  //设置下一级领导
  public void setNetLeader(Leader leader) {
   this.nextLeader=leader;
  }
  //业务方法处理请假条
  public abstract void handler(LeaveRequest leaveRequest) ;
}

public class Director extends Leader{

 public Director(String leaderName) {
  super(leaderName);
 }

 @Override
 public void handler(LeaveRequest leaveRequest) {
    if(leaveRequest.getLeaveDays()<3) {
     System.out.println("员工:"+leaveRequest.getEmpName()+",请假:"+leaveRequest.getLeaveDays()+",原因:"+leaveRequest.getReason());
     System.out.println("主任:"+this.leaderName+",审核通过!");
    }else {
     if(this.nextLeader!=null) {
      nextLeader.handler(leaveRequest);
     }else {
      System.out.println("此员工可能想辞职");
     }
    }
 }

}

public class Manager extends Leader{

 public Manager(String leaderName) {
  super(leaderName);
 }

 @Override
 public void handler(LeaveRequest leaveRequest) {
    if(leaveRequest.getLeaveDays()<10) {
     System.out.println("员工:"+leaveRequest.getEmpName()+",请假:"+leaveRequest.getLeaveDays()+",原因:"+leaveRequest.getReason());
     System.out.println("经理:"+this.leaderName+",审核通过!");
    }else {
     if(this.nextLeader!=null) {
      nextLeader.handler(leaveRequest);
     }else {
      System.out.println("此员工可能想辞职");
     }
    }
 }

}

public class GeneralManager extends Leader{

 public GeneralManager(String leaderName) {
  super(leaderName);
 }

 @Override
 public void handler(LeaveRequest leaveRequest) {
    if(leaveRequest.getLeaveDays()<30) {
     System.out.println("员工:"+leaveRequest.getEmpName()+",请假:"+leaveRequest.getLeaveDays()+",原因:"+leaveRequest.getReason());
     System.out.println("总经理:"+this.leaderName+",审核通过!");
    }else {
     if(this.nextLeader!=null) {
      nextLeader.handler(leaveRequest);
     }else {
      System.out.println("此员工可能想辞职");
     }
    }
 }

}

public class Client {

 public static void main(String[] args) {
   Leader a=new Director("张三");
   Leader b=new Manager("李四");
   Leader c=new GeneralManager("王五");
   
   //开始组织责任链
   a.setNetLeader(b);
   b.setNetLeader(c);
   
   LeaveRequest req1=new LeaveRequest("TOM", 2, "抓JACK");
   a.handler(req1);
 }

}