当前位置: 首页 > article >正文

责任链设计模式

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;
    }
}
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("流程结束 !");
        }


    }

}
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("总经理审批:同意");
    }
}
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("部门经理审批:同意");
    }
}
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("小组长审批:同意");
    }
}
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);
    }
}


http://www.kler.cn/a/154747.html

相关文章:

  • RK3588 C++ 多线程运行
  • 安全,服务器证书和SSL连接
  • ubuntu下openssl签名证书制作流程及验证demo
  • 卡尔曼滤波:从理论到应用的简介
  • 【数据分享】全国农产品成本收益资料汇编(1953-2024)
  • Molecular signatures database (MSigDB) 3.0
  • 机器学习笔记 - 异常检测之OneClass SVM算法简述
  • etlbox.3.1.0 for NET 轻量级 ETL数据集成库 Crack
  • 熬夜会秃头——beta冲刺Day3
  • scrapyd及gerapy的使用及docker-compse部署
  • UI上传组件异步上传更改为同步
  • arXiv学术速递笔记11.28
  • JVM运行时数据区域
  • 【2023CANN训练营第二季】——Ascend C自定义算子工程介绍及实验
  • Three.js的THREE.LOD如何加载gltf模型
  • 华为鲲鹏+银河麒麟V10编译FreeSWITCH1.10.9
  • 配置typroa上传图片到gitee
  • 环境部署的学习笔记(Docker)
  • css所有属性介绍
  • 数据结构-二叉树(2)
  • 159.库存管理(TOPk问题!)
  • 戴尔科技推出全新96核Precision 7875塔式工作站
  • 数据结构(超详细讲解!!)第二十五节 树与森林
  • Nginx实现多虚拟主机配置
  • MySQL海量数据配置优化教程
  • 通过两个css属性提升长列表渲染效率