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

相关推荐
码匠许师傅13 小时前
【设计模式精讲】24.观察者模式(Observer)
c++·观察者模式·设计模式·uml
小程故事多_8017 小时前
从快速迭代到稳定存续,Google五大设计模式重构长效AI智能体落地逻辑
人工智能·设计模式·重构
码匠许师傅1 天前
【设计模式精讲】22.中介者模式(Mediator)
c++·设计模式·软件工程·uml·中介者模式
2401_868534782 天前
网规备考_2.4 路由协议
c++·设计模式
cpp_learner2 天前
C++ 实现责任链模式(Chain of Responsibility):从一堆 if-else 到可插拔的处理管道
c++·设计模式
码匠许师傅2 天前
【设计模式精讲】23.备忘录模式(Memento)
c++·设计模式·软件工程·uml·备忘录模式
新知图书2 天前
第8章 多智能体协同
人工智能·设计模式·智能体
京师20万禁军教头2 天前
39面向对象(高级)-设计模式
java·开发语言·设计模式
新知图书3 天前
3.5 智能体设计模式1:反应式智能体与自动导航案例
人工智能·设计模式·智能体
码匠许师傅3 天前
【设计模式精讲】21.迭代器模式(Iterator)
c++·设计模式·rpc·迭代器模式·软件工程·uml