Java教程

java实现责任链模式的小demo

本文主要是介绍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我是自定义处理器,我处理了

 

这篇关于java实现责任链模式的小demo的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!