当前位置:网站首页>职责链模式(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);
}
}
边栏推荐
猜你喜欢
C语言*小白的探险历程
Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!
浅析深度学习在图像处理中的应用趋势及常见技巧
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
CVPR 2022 | 从人体网格预测骨架,是真正的生理学骨架!
高级转录组分析和R数据可视化火热报名中(2022.10)
【Idea series】idea configuration
MySQL: Integrity Constraints and Table Design Principles
HCIP 交换实验
Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
随机推荐
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
什么是终端特权管理
Mobile open source low code tools beeware and kivy
win8和win10下,visual studio 2008 调试出现无响应的卡死问题解决
C语言*小白的探险历程
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三
无代码平台附件上传入门教程
如何直击固定资产管理的难题?
Rust 入门指南 (用 WASM 开发第一个 Web 页面)
怎么禁止textarea拉伸
【Idea series】idea configuration
利用pytest hook函数实现自动化测试结果推送企业微信
标准C语言学习总结12
开源一夏|ArkUI如何自定义弹窗(eTS)
开源一夏|ArkUI如何自定义弹窗(eTS)
ThreadLocal详细分析
Why are all hotel bathrooms transparent?
Introduction to Mysql storage engine
Multimedia and Internet of Things technology make the version "live" 129 vinyl records "Centennial Voice"
DB2查看执行过长的SQL