当前位置:网站首页>行为型模式之责任链模式
行为型模式之责任链模式
2022-07-25 23:52:00 【w͏l͏j͏】
责任链模式
是什么?
责任链(Chain of Responsibility)模式:为了避免请求发送者与多个请求处理者耦合在一起,于是将所有请求的处理者通过前一对象记住其下一个对象的引用而连成一条链;当有请求发生时,可将请求沿着这条链传递,直到有对象处理它为止。生活中也有类似的例子,比如采购审批流程、请假流程等;在计算机软硬件中也是很常见,比如拦截器,过滤器,都可以考虑责任链模式来实现。
在责任链模式中,客户只需要将请求发送到责任链上即可,无须关心请求的处理细节和请求的传递过程,请求会自动进行传递。所以责任链将请求的发送者和请求的处理者解耦了。
优点
- 降低了对象之间的耦合度。该模式使得一个对象无须知道到底是哪一个对象处理其请求以及链的结构,发送者和接收者也无须拥有对方的明确信息。
- 增强了系统的可扩展性。可以根据需要增加新的请求处理类,满足开闭原则。‘
- 增强了给对象指派职责的灵活性。当工作流程发生变化,可以动态地改变链内的成员或者调动它们的次序,也可动态地新增或者删除责任。
- 责任链简化了对象之间的连接。每个对象只需保持一个指向其后继者的引用,不需保持其他所有处理者的引用。
- 责任分担。每个类只需要处理自己该处理的工作,不该处理的传递给下一个对象完成,明确各类的责任范围,符合类的单一职责原则。
缺点
- 不能保证每个请求一定被处理。由于一个请求没有明确的接收者,所以不能保证它一定会被处理,该请求可能一直传到链的末端都得不到处理。
- 对比较长的职责链,请求的处理可能涉及多个处理对象,系统性能将受到一定影响,
- 职责链建立的合理性要靠客户端来保证,增加了客户端的复杂性,可能会由于职责链的错误设置而导致系统出错,如可能会造成循环调用。
模式的结构


实现
以请假审批模块来简单模拟一下责任链模式的应用。
场景:学生请假
学生请假小于2天,班主任可以直接审批;学生请假小于7天,系主任可以直接审批;小于10天,院长可以审批,其余情况不予审批。
代码实现
班主任,系主任,院长
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname Leader * @Description 抽象出领导类 * @Date 7/24/2022 10:35 PM */
public abstract class Leader {
// 下一个责任的引用
private Leader next;
public Leader getNext() {
return next;
}
public void setNext(Leader next) {
this.next = next;
}
/** * 处理学生的请假请求 * @param day 学生请假的天数 */
public abstract void handleRequest(int day);
}
// 班主任
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname ClassAdviser * @Description 班主任 * @Date 7/24/2022 10:42 PM */
public class ClassAdviser extends Leader{
@Override
public void handleRequest(int day) {
if(day <= 2){
System.out.println("班主任批准:"+day);
}else{
if(getNext() != null){
getNext().handleRequest(day);
}
else{
System.out.println("请假天数太多,不予批准");
}
}
}
}
// 系主任
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname ClassAdviser * @Description 系主任 * @Date 7/24/2022 10:42 PM */
public class DepartmentHead extends Leader{
@Override
public void handleRequest(int day) {
if(day <= 7){
System.out.println("系主任批准:"+day);
}else{
if(getNext() != null){
getNext().handleRequest(day);
}
else{
System.out.println("请假天数太多,不予批准");
}
}
}
}
// 院长
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname ClassAdviser * @Description 院长 * @Date 7/24/2022 10:42 PM */
public class Dean extends Leader{
@Override
public void handleRequest(int day) {
if(day <= 10){
System.out.println("院长批准:"+day);
}else{
if(getNext() != null){
getNext().handleRequest(day);
}
else{
System.out.println("请假天数太多,不予批准");
}
}
}
}
客户端
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname Client * @Description 场景模拟 * @Date 7/24/2022 10:51 PM */
public class Client {
public static void main(String[] args) {
// 构建责任链
Leader leader1 = new ClassAdviser();
Leader leader2 = new DepartmentHead();
Leader leader3 = new Dean();
// leader1(ClassAdviser) -> leader2(DepartmentHead) ->leader3(Dean)
leader1.setNext(leader2);
leader2.setNext(leader3);
leader3.setNext(null);
// 模拟请假2天
leader1.handleRequest(2);
// 模拟请假8天
leader1.handleRequest(7);
// 模拟请假9天
leader1.handleRequest(9);
// 模拟请假12天
leader1.handleRequest(12);
}
}
output:
班主任批准:2
系主任批准:7
院长批准:9
请假天数太多,不予批准
边栏推荐
- 生成随机数random学习之uniform_int_distribution,uniform_real_distribution
- LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
- Zhiniu stock -- 09
- Docker installation redis-5.0.12 (remote access)
- Programmer interview Golden Classic 4.12 summation path
- 2022-07-18 study notes of group 5 self-cultivation class (every day)
- Optimize the browsing experience of yandere/konachan site with user scripts
- 152. Product maximum subarray - dynamic programming
- Taobao Search case
- C - readonly and const keywords
猜你喜欢

ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)

Good news under the epidemic

Docker 安装 Redis-5.0.12(远程访问)

【MUDUO】EventLoop事件循环

Query commodity cases (operate data with array addition method) / key points

ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve

2022-07-18 study notes of group 5 self-cultivation class (every day)

面试重点——传输层的TCP协议

Taobao Search case

获取马蜂窝酒店数据
随机推荐
S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
Fixed and alternate sequential execution of modes
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
[learning notes] unreal 4 engine introduction (III)
Array merge method: concat()
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
Good news under the epidemic
Duplicate numbers in array
Find the cause of program dead cycle through debugging
JS synchronization and asynchrony
Macro task, micro task and event cycle mechanism
《数据密集型应用系统设计》 - 应用系统概览
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
Song of statistics lyrics
【MUDUO】EventLoopThreadPool
C language (high level) program environment and preprocessing
[learning notes] solid works operation record
[testing technology automated testing pytest] basic summary of pytest
Get the data of Mafeng Hotel
