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

相关推荐
香芋Yu30 分钟前
【深度学习教程——01_深度基石(Foundation)】05_数据太多怎么吃?Mini-batch训练的设计模式
深度学习·设计模式·batch
进击的小头4 小时前
设计模式组合应用:传感器数据采集与处理系统
c语言·设计模式
茶本无香5 小时前
设计模式之十一—桥接模式:解耦抽象与实现的艺术
设计模式·桥接模式
短剑重铸之日5 小时前
《设计模式》第四篇:观察者模式
java·后端·观察者模式·设计模式
七夜zippoe7 小时前
API网关设计模式实战 Spring Cloud Gateway路由过滤限流深度解析
java·设计模式·gateway·路由·api网关
yangpipi-1 天前
2. 设计模式之结构型模式
设计模式
进击的小头1 天前
设计模式组合应用:嵌入式通信协议栈
c语言·设计模式·策略模式
致Great1 天前
智能体的设计模式探讨
设计模式
BD_Marathon1 天前
设计模式——单一职责原则
设计模式·单一职责原则
stevenzqzq1 天前
Slot API 设计模式
设计模式·compose