当前位置:网站首页>职责链模式(responsibilitychain)
职责链模式(responsibilitychain)
2022-08-04 10:52:00 【pmc0_0】
问题引入
一个人请假,根据请假天数的不同,应该由不同的人处理
模式引入
public abstract class Handle {
Handle nextHandle;
String name;
public Handle(String name) {
this.name = name;
}
/** * @Description: 设置下一个处理者,为了让子类形成链条 * @Param nextHandle: * @return void * @date 2022/7/31 10:44 */
public void setNextHandle(Handle nextHandle) {
this.nextHandle = nextHandle;
}
/** * @Description: 假期请求处理抽象方法,具体实现交给子类 * @Param vacationRequest: * @return void * @date 2022/7/31 10:46 */
public abstract void processRequest(VacationRequest vacationRequest);
}
- 具体子类
public class GroupLeader extends Handle {
public GroupLeader(String name) {
super(name);
}
@Override
public void processRequest(VacationRequest vacationRequest) {
if (vacationRequest.getVacationDay() <= 3) {
System.out.println(vacationRequest.getId() + "被组长处理");
} else {
// 将请求传递给下一个执行链
nextHandle.processRequest(vacationRequest);
}
}
}
- 具体子类
public class Manager extends Handle{
public Manager(String name) {
super(name);
}
@Override
public void processRequest(VacationRequest vacationRequest) {
if (vacationRequest.getVacationDay() > 3 && vacationRequest.getVacationDay() <= 30) {
System.out.println(vacationRequest.getId() + "被经理处理了");
} else {
nextHandle.processRequest(vacationRequest);
}
}
}
- 具体子类
public class Chairman extends Handle {
public Chairman(String name) {
super(name);
}
@Override
public void processRequest(VacationRequest vacationRequest) {
if (vacationRequest.getVacationDay() > 30) {
System.out.println(vacationRequest.getId() + "被董事长处理了");
} else {
nextHandle.processRequest(vacationRequest);
}
}
}
- 封装一个请求类
public class VacationRequest {
private int type = 0;
private int vacationDay; // 请假天数
private int id;
public VacationRequest(int type, int vacationDay, int id) {
this.type = type;
this.vacationDay = vacationDay;
this.id = id;
}
public int getType() {
return type;
}
public int getVacationDay() {
return vacationDay;
}
public int getId() {
return id;
}
}
调用测试
public class Client {
public static void main(String[] args) {
// 创建一个请求
VacationRequest vacationRequest = new VacationRequest(1, 130, 1111);
// 创建相关负责人
GroupLeader groupLeader = new GroupLeader("组长");
Manager manager = new Manager("经理");
Chairman chairman = new Chairman("懂事长");
// 设置链条,形成环路
groupLeader.setNextHandle(manager);
manager.setNextHandle(chairman);
chairman.setNextHandle(groupLeader);
groupLeader.processRequest(vacationRequest);
}
}
边栏推荐
- MySQL: Integrity Constraints and Table Design Principles
- 深度学习100例 —— 卷积神经网络(CNN)天气识别
- 2万字50张图玩转Flink面试体系
- 知网网站地址更换
- datax oracle to oracle离线json文件
- 知其然,知其所以然,JS 对象创建与继承
- 开源一夏|ArkUI如何自定义弹窗(eTS)
- datax oracle to oracle incremental synchronization
- Win11 file types, how to change?Win11 modify the file suffix
- [easyUI]修改datagrid表格中的值
猜你喜欢
随机推荐
【Idea系列】idea配置
[easyUI]修改datagrid表格中的值
标准C语言学习总结12
使用.NET简单实现一个Redis的高性能克隆版(二)
Jina 实例秀|基于神经搜索的网络安全威胁检测(一)
Four ways to traverse a Map
怎么禁止textarea拉伸
遍历Map的四种方法
MySQL之my.cnf配置文件
Jenkins User Manual (1) - Software Installation
Oracle中对临时表空间执行shrink操作
JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
北京大学,新迎3位副校长!其中一人为中科院院士!
无代码平台多项选择入门教程
vscode插件设置——Golang开发环境配置
Use pytest hook function to realize automatic test result push enterprise WeChat
学会使用set和map的基本接口
C language * Xiaobai's adventure
ThreadLocal详细分析