设计模式——行为型——责任链模式Chain Of Responsibility

请求类

java 复制代码
public class ApproverRequest {
    private int type;//请求批准的类型
    private float price;//请求的金额
    private int id;//请求的编号
}

审批人抽象类

java 复制代码
public abstract class ApproverPerson {
    protected ApproverPerson next;
    protected String name;

    //审批过程
    public abstract void processRequest(ApproverRequest request);
}

教导主任类

java 复制代码
public class DepartmentApprover extends ApproverPerson {
    @Override
    public void processRequest(ApproverRequest request) {
        System.out.println("教导主任正在处理批准...");

        if (request.getPrice() <= 5000) {
            System.out.println("请求编号 id= " + request.getId() + " 被教导主任 " + this.name + " 处理");
        } else {
            System.out.println("金额过大,教导主任处理不了,交给院长处理...");
            next.processRequest(request);
        }
    }
}

院长类

java 复制代码
public class CollegeApprover extends ApproverPerson {
    @Override
    public void processRequest(ApproverRequest request) {
        System.out.println("院长正在处理批准...");

        if (request.getPrice() > 5000 && request.getPrice() <= 10000) {
            System.out.println("请求编号 id= " + request.getId() + " 被院长 " + this.name + " 处理");
        } else {
            System.out.println("金额过大,院长处理不了,交给副校长处理...");
            next.processRequest(request);
        }
    }
}

副校长类

java 复制代码
public class ViceSchoolMasterApprover extends ApproverPerson {
    @Override
    public void processRequest(ApproverRequest request) {
        System.out.println("副校长正在处理批准...");

        if (request.getPrice() > 10000 && request.getPrice() <= 15000) {
            System.out.println("请求编号 id= " + request.getId() + " 被副校长 " + this.name + " 处理");
        } else {
            System.out.println("金额过大,副校长处理不了,交给校长处理...");
            next.processRequest(request);
        }
    }
}

校长类

java 复制代码
public class SchoolMasterApprover extends ApproverPerson {
    @Override
    public void processRequest(ApproverRequest request) {
        System.out.println("校长正在处理批准...");

        System.out.println("请求编号 id= " + request.getId() + " 被校长 " + this.name + " 处理");
    }
}

演示类

java 复制代码
public class Demo {
    public static void main(String[] args) {
        ApproverRequest request = new ApproverRequest(1, 20000, 1001);
        ApproverPerson department = new DepartmentApprover("张主任");
        ApproverPerson college = new CollegeApprover("李院长");
        ApproverPerson viceSchoolMaster = new ViceSchoolMasterApprover("王副校长");
        ApproverPerson schoolMaster = new SchoolMasterApprover("佟校长");

        //设置当前审批人的指针,指向下一个审批人
        department.setNext(college);
        college.setNext(viceSchoolMaster);
        viceSchoolMaster.setNext(schoolMaster);

        department.processRequest(request);
    }
}

演示结果

为了使博客的代码更简洁,我把构造方法和set/get方法省略了

相关推荐
xiaolyuh1233 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
GISer_Jing14 小时前
智能体工具使用、规划模式
人工智能·设计模式·prompt·aigc
GISer_Jing14 小时前
AI Agent:学习与适应、模型上下文协议
人工智能·学习·设计模式·aigc
小马爱打代码15 小时前
MyBatis设计模式:构建者、工厂、代理模式
设计模式·mybatis·代理模式
月明长歌15 小时前
Javasynchronized 原理拆解:锁升级链路 + JVM 优化 + CAS 与 ABA 问题(完整整合版)
java·开发语言·jvm·安全·设计模式
会员果汁16 小时前
12.设计模式-状态模式
设计模式·状态模式
Yu_Lijing16 小时前
基于C++的《Head First设计模式》笔记——抽象工厂模式
c++·笔记·设计模式
会员果汁19 小时前
13.设计模式-适配器模式
设计模式·适配器模式
GISer_Jing1 天前
AI:多智能体协作与记忆管理
人工智能·设计模式·aigc
雨中飘荡的记忆1 天前
责任链模式实战应用:从理论到生产实践
设计模式