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

相关推荐
Momentary_SixthSense1 天前
设计模式之工厂模式
java·开发语言·设计模式
Java码农也是农1 天前
Multi-Agent 系统设计模式
设计模式·agent·multi-agent
sg_knight1 天前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
workflower1 天前
深度学习是通用型人工智能的基础
人工智能·深度学习·设计模式·软件工程·软件构建·制造
Meme Buoy1 天前
11.3设计模式-新
设计模式
cmpxr_1 天前
【单片机】常用设计模式
单片机·嵌入式硬件·设计模式
无籽西瓜a1 天前
【西瓜带你学设计模式 | 第十五期 - 策略模式】策略模式 —— 算法封装与动态替换实现、优缺点与适用场景
java·后端·设计模式·软件工程·策略模式
Kel2 天前
Claude Code 架构深度剖析:从终端输入到大模型响应的完整过程
人工智能·设计模式·架构
￰meteor2 天前
23种设计模式 -【观察者】
设计模式
妙蛙种子3112 天前
【Java设计模式 | 创建者模式】 抽象工厂模式
java·开发语言·后端·设计模式·抽象工厂模式