手写实现一个责任链模式仿照了netty责任链

public class Leave {

    private String userName;

    private int  leaveDays;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getLeaveDays() {
        return leaveDays;
    }

    public void setLeaveDays(int leaveDays) {
        this.leaveDays = leaveDays;
    }
}
public abstract class LeaveHandler {

    protected static final int MIN = 1;

    protected static final int MIDDLE = 3;

    protected LeaveHandler nextHandler;

    public abstract void execute(Leave leave);

}
public class HeadLeaveHandler extends LeaveHandler{
    @Override
    public void execute(Leave leave) {
        System.out.println("HeadLeaveHandler 开始流程处理..........");
        nextHandler.execute(leave);
    }
}
public class LeaderLeaveHandler extends LeaveHandler{

    @Override
    public void execute(Leave leave) {
        System.out.println("LeaderLeaveHandler 流程已处理.....");
        if(leave.getLeaveDays() <=  MIN){
            System.out.println("LeaderLeaveHandler 流程已完结.....");
            return;
        }
        nextHandler.execute(leave);
    }
}
public class DeptLeaveHandler extends LeaveHandler{

    @Override
    public void execute(Leave leave) {
        System.out.println("DeptLeaveHandler 流程已处理.....");
        if(leave.getLeaveDays() <= MIDDLE){
            System.out.println("DeptLeaveHandler 流程已完结.....");
            return;
        }
        nextHandler.execute(leave);
    }
}
public class BossLeaveHandler extends LeaveHandler{

    @Override
    public void execute(Leave leave) {
        System.out.println("BossLeaveHandler 流程已处理....");
        System.out.println("BossLeaveHandler 流程已完结....");
        return;
    }
}
public  class InitLeaveHandler{

    private  HandlerNode handlerNode = new HandlerNode();

    InitLeaveHandler addLeft(LeaveHandler handler){
        handlerNode.addLeft(new HandlerNode(handler));
        return this;
    }

    InitLeaveHandler addRight(LeaveHandler handler){
        handlerNode.addRight(new HandlerNode(handler));
        return this;
    }


    public void execute(Leave leave) {
        handlerNode.handler.execute(leave);
    }

    public void getAll(Consumer consumer){
        HandlerNode temp = handlerNode;
        while (temp != null){
            consumer.accept(temp);
            temp = temp.next;
        }
    }
    class HandlerNode{

        HandlerNode next;

        LeaveHandler handler;

        public HandlerNode(){}

        private HandlerNode(LeaveHandler leaveHandler){
            this.handler = leaveHandler;
        }

        public void addRight(HandlerNode node){
            if(handlerNode.handler == null){
                handlerNode = node;
                return;
            }
            HandlerNode tailNode = getTail(handlerNode);
            tailNode.handler.nextHandler = node.handler;
            tailNode.next = node;
        }

        public HandlerNode getTail(HandlerNode node){
            if(node.next == null || node.next.handler == null){
                return node;
            }
            return getTail(node.next);
        }

        public void addLeft(HandlerNode node){
            if(handlerNode.handler == null){
                handlerNode = node;
                return;
            }
            HandlerNode temp = handlerNode;
            handlerNode = node;
            handlerNode.next = temp;
        }
    }
}
public class HandlerV1Test {
    public static void main(String[] args) {
        Leave leave = new Leave();
        leave.setUserName("张三");
        leave.setLeaveDays(10);
        InitLeaveHandler initLeaveHandler = new InitLeaveHandler()
                .addRight(new HeadLeaveHandler())
                .addRight(new LeaderLeaveHandler())
                .addRight(new DeptLeaveHandler())
                .addRight(new BossLeaveHandler());
        initLeaveHandler.getAll(h -> System.out.println(h.handler));
        initLeaveHandler.execute(leave);

    }
}

欢迎小伙伴一起探讨设计模式

源码已经提交到码云  https://gitee.com/tianji_luhaichuan/pay/tree/master/java-base 

你可能感兴趣的:(技术类,java)