设计模式——行为型——责任链模式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方法省略了

相关推荐
董员外2 小时前
RAG 系统进化论(七):Multimodal RAG(多模态 RAG),当知识存在于表格、图片和页面中
人工智能·后端·设计模式
大山同学2 小时前
第 2 篇(03-04 章):代理设计原则与工具使用设计模式
设计模式
啦啦啦啦啦zzzz1 天前
设计模式:桥接模式和组合模式
c++·设计模式·组合模式·桥接模式
莫得感情 o1 天前
设计模式 03 · 工厂方法模式
设计模式·工厂方法模式
cyforkk1 天前
高并发核心设计模式:Single-Flight 原理与实战
设计模式
饼干哥哥2 天前
字节Seedance2.5终于上线,这次在收割谁?
人工智能·设计模式·前端框架
董员外2 天前
RAG 系统进化论(六):GraphRAG(基于知识图谱的 RAG),从相似文本走向实体关系
人工智能·后端·设计模式
鬼鬼鬼2 天前
从 Prompt 到 Harness:企业级 Agent 工程的完整演进之路
设计模式·架构·ai编程
啦啦啦啦啦zzzz2 天前
设计模式:原型模式
c++·设计模式·原型模式
董员外2 天前
RAG 系统进化论(五):Corrective RAG 与 Self-RAG,让系统发现并纠正错误
人工智能·后端·设计模式