当前位置:网站首页>职责链模式(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);
}
}
边栏推荐
猜你喜欢

华为开源:聚焦开源基础软件,共建健康繁荣生态

北京大学,新迎3位副校长!其中一人为中科院院士!

无代码平台数字入门教程

MySQL core SQL: SQL structured query statements, library, table operation, CRUD

Small program containers accelerate the construction of an integrated online government service platform

C#/VB.NET:在 Word 中设置文本对齐方式

Introduction to Mysql storage engine

在 .NET MAUI 中如何更好地自定义控件

Use pytest hook function to realize automatic test result push enterprise WeChat

图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)
随机推荐
无代码平台单项选择入门教程
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
There are 12 balls, including 11 weight, only one, don't know is light or heavy. Three times in balance scales, find out the ball.
知其然,知其所以然,JS 对象创建与继承
Super Learning Method
无代码平台数字入门教程
helm安装
iMeta | 德国国家肿瘤中心顾祖光发表复杂热图(ComplexHeatmap)可视化方法
低代码是开发的未来吗?浅谈低代码开发平台的发展现状及未来趋势
What is the terminal privilege management
cubemx stm32 afm3000模块 气体流量传感器 驱动代码
知网网站地址更换
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
MATLAB程序设计与应用 3.2 矩阵变换
RL78开发环境
winform 在Datatable插入一笔数据
RL78 development environment
强烈推荐一款优秀且通用的后台管理系统
AWS Lambda related concepts and implementation approach
Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)