责任链设计模式

java 复制代码
package com.jmj.pattern.responsibility;

/**
 * 请假条类
 */
public class LeaveRequest {
    //姓名
    private String name;
    //请假天数
    private int num;
   //请假内容
    private String content;

    public LeaveRequest(String name, int num, String content) {
        this.name = name;
        this.num = num;
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
java 复制代码
package com.jmj.pattern.responsibility;

public abstract class Handler {
    protected final static int NUM_ONE=1;
    protected final static int NUM_THREE=3;
    protected final static int NUM_SEVEN=7;

    //该领导处理的请求天数区间
    private int numStart;
    private int numEnd;


    private Handler nextHandler;

    public Handler(int numStart) {
        this.numStart = numStart;
    }

    public Handler(int numStart, int numEnd) {
        this.numStart = numStart;
        this.numEnd = numEnd;
    }

    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    //各级领导处理请假条的方法
    protected abstract void handleLeave(LeaveRequest leave);

    public final void submit(LeaveRequest leave){

        if (this.nextHandler!=null&&leave.getNum()>this.numEnd){
            this.nextHandler.submit(leave);
        }else {
            this.handleLeave(leave);
            System.out.println("流程结束 !");
        }


    }

}
java 复制代码
package com.jmj.pattern.responsibility;

public class GeneralManager extends Handler{
    public GeneralManager(){
        super(Handler.NUM_THREE,Handler.NUM_SEVEN);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println(leave.getName()+"请假"+leave.getNum()+"天,"+leave.getContent()+"。");
        System.out.println("总经理审批:同意");
    }
}
java 复制代码
package com.jmj.pattern.responsibility;

public class Manager extends Handler{
    public Manager(){
        super(Handler.NUM_ONE,Handler.NUM_THREE);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println(leave.getName()+"请假"+leave.getNum()+"天,"+leave.getContent()+"。");
        System.out.println("部门经理审批:同意");
    }
}
java 复制代码
package com.jmj.pattern.responsibility;

public class GroupLeader extends Handler{
    public GroupLeader(){
        super(0,Handler.NUM_ONE);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println(leave.getName()+"请假"+leave.getNum()+"天,"+leave.getContent()+"。");
        System.out.println("小组长审批:同意");
    }
}
java 复制代码
package com.jmj.pattern.responsibility;

public class Client {
    public static void main(String[] args) {
        LeaveRequest leaveRequest = new LeaveRequest("张三", 5, "身体不适");
        GroupLeader groupLeader = new GroupLeader();
        Manager manager = new Manager();
        GeneralManager generalManager = new GeneralManager();
        groupLeader.setNextHandler(manager);

        manager.setNextHandler(generalManager);

        groupLeader.submit(leaveRequest);
    }
}
相关推荐
风象南2 小时前
SpringBoot中6种自定义starter开发方法
java·spring boot·后端
mghio11 小时前
Dubbo 中的集群容错
java·微服务·dubbo
千千寰宇12 小时前
[设计模式/Java/多线程] 设计模式之单例模式【9】
设计模式·操作系统-进程/线程/并发
咖啡教室16 小时前
java日常开发笔记和开发问题记录
java
咖啡教室16 小时前
java练习项目记录笔记
java
鱼樱前端16 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea17 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea17 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
我不会编程55519 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python