java实现责任链模式的小demo

//一个请假请求
public class LeaveRequest {
    private int leaveDays;

    private String name;

    public void leave(){
        System.out.println("走完了流程都没人处理我");
    }
    
    //省略getter、setter      
}
//一个请假的处理接口,具体的处理类都要实现这个接口
@FunctionalInterface
public interface LeaveHandler {
    // 直属主管审批处理的请假天数
    int MIN = 3;

    // 部门经理需要处理的请假天数
    int MIDDLE = 10;

    // 总经理审批
    int MAX = 30;

    void handRequest(LeaveRequest leaveRequest, FilterChain filterChain);
}
//直属主管处理类
public class SupervisorHandler implements LeaveHandler{

    @Override
    public void handRequest(LeaveRequest leaveRequest, FilterChain filterChain) {
        if (leaveRequest.getLeaveDays() < MIN){
            System.out.println("请假天数" + leaveRequest.getLeaveDays() + "我是直属主管,我处理了");
            return;
        }
        System.out.println("主管处理不了,下一个");
        filterChain.doFilter();
    }
}

//部门经理处理类
public class DeptManagerHandler implements LeaveHandler{

    @Override
    public void handRequest(LeaveRequest leaveRequest, FilterChain filterChain) {
        if (leaveRequest.getLeaveDays() <= MIDDLE){
            System.out.println("请假天数" + leaveRequest.getLeaveDays() + "我是部门经理,我处理了");
            return;
        }
        System.out.println("经理处理不了,下一个");
        filterChain.doFilter();
    }
}

//总经理处理类
public class GeneralManagerHandler implements LeaveHandler{

    @Override
    public void handRequest(LeaveRequest leaveRequest, FilterChain filterChain) {
        if (leaveRequest.getLeaveDays() <= MAX){
            System.out.println("请假天数" + leaveRequest.getLeaveDays() + "我是总经理,我处理了");
            return;
        }
        System.out.println("总经理处理不了,下一个");
        filterChain.doFilter();
    }
}
//过滤器链,把各个处理类串联起来
public class FilterChain {
    private int index = 0;

    private LeaveRequest leaveRequest;

    private List<LeaveHandler> leaveHandlerList = new ArrayList<LeaveHandler>();

    public FilterChain(LeaveRequest leaveRequest){
        this.leaveRequest = leaveRequest;
    }

    public FilterChain addHandler(LeaveHandler leaveHandler){
        if (null == leaveHandler) return null;
        this.leaveHandlerList.add(leaveHandler);
        return this;
    }

    public void doFilter(){
        if(index == leaveHandlerList.size()) {
            leaveRequest.leave();
            return;
        }
      leaveHandlerList.get(index++).handRequest(leaveRequest,this); } }
public class Main {
    public static void main(String[] args){
        LeaveRequest leaveRequest = new LeaveRequest(49,"小明");

        FilterChain filterChain = new FilterChain(leaveRequest);

        filterChain.addHandler(new SupervisorHandler())
                   .addHandler(new DeptManagerHandler())
                   .addHandler(new GeneralManagerHandler())
                   //使用lambda表达式创建自定义处理类
                   .addHandler((LeaveRequest request, FilterChain filter) -> {
                       if (request.getLeaveDays() <= 50){
                           System.out.println("请假天数" + request.getLeaveDays() + "我是自定义处理器,我处理了");
                           return;
                       }
                       System.out.println("自定义处理不了,下一个");
                       filter.doFilter();
                   })
                   .doFilter();
    }
}    
输出:
主管处理不了,下一个
经理处理不了,下一个
总经理处理不了,下一个
请假天数49我是自定义处理器,我处理了

 

上一篇:Filter过滤器


下一篇:C#在OpenFileDialog中不显示过滤器扩展