责任链模式简介
- 责任链模式(chain responsibility)是为了避免请求发送者与多个请求处理者耦合在一起,将所有请求的处理者,通过前一对象记住其下一个对象的引用,而连成一条链,当有请求发生时候,可将请求沿着这一条链传递,直到有对象处理它为止。
结构
- 1.抽象处理者角色(Abstract Handler):定义一个处理请求的接口,包含抽象处理方法和一个后续链接。
- 2.具体处理者角色(concrete Handler):实现抽象处理者角色的处理方法,判断能否处理本次请求,如果可以处理请求则处理,不能就转给它的后续者。
- 3.客户类角色(Client):创建处理类,并向链头的具体处理者对象提交请求,它不关心处理细节和请求的传递过程。
UML图

具体实现
例子:员工请假,1-3天小组长有权限批准,3-7天部门经理有权限审批,7天以上需要总经理审批,用责任链模式实现此场景功能。
UML图

代码实现
- 抽象处理角色
package com.xxliao.pattern.behavioral.chain_responsibility.demo;
/**
* @author xxliao
* @description: 抽象处理类
* @date 2024/5/27 17:28
*/
public abstract class Handler {
protected final static int NUM_ONE = 1;
protected final static int NUM_THREE = 3;
protected final static int NUM_SEVEN = 7;
// 该处理者可以处理的取件
private int startNum;
private int endNum;
// 上级领导
private Handler nextHandler;
// 设置请假天数
public Handler(int startNum) {
this.startNum = startNum;
}
// 设置请假天数范围
public Handler(int startNum, int endNum) {
this.startNum = startNum;
this.endNum = endNum;
}
// 设置上级领导
public void setNextHandler(Handler nextHandler) {
this.nextHandler = nextHandler;
}
/**
* @description 提交请假条
* @author xxliao
* @date 2024/5/27 17:32
*/
public final void submit(LeaveRequest leave) {
if(0 == this.startNum) {
return;
}
if(leave.getNum() >= this.startNum) {
this.handleLeave(leave);
//如果还有上级 并且请假天数超过了当前领导的处理范围
if(null != this.nextHandler && leave.getNum() > endNum){
this.nextHandler.submit(leave);//继续提交
} else {
System.out.println("流程结束");
}
}
}
protected abstract void handleLeave(LeaveRequest leave);
}
- 具体处理角色
package com.xxliao.pattern.behavioral.chain_responsibility.demo;
/**
* @author xxliao
* @description: 具体处理者 -组长
* @date 2024/5/27 17:37
*/
public class GroupLeader extends Handler{
public GroupLeader() {
//小组长处理1-3天的请假
super(Handler.NUM_ONE, Handler.NUM_THREE);
}
@Override
protected void handleLeave(LeaveRequest leave) {
System.out.println(leave.getName() + "请假" + leave.getNum() +
"天," + leave.getContent() + "。");
System.out.println("小组长审批:同意。");
}
}
package com.xxliao.pattern.behavioral.chain_responsibility.demo;
/**
* @author xxliao
* @description: 具体处理类
* @date 2024/5/27 17:38
*/
public class Manager extends Handler{
public Manager() {
//部门经理处理3-7天的请假
super(Handler.NUM_THREE, Handler.NUM_SEVEN);
}
@Override
protected void handleLeave(LeaveRequest leave) {
System.out.println(leave.getName() + "请假" + leave.getNum() +
"天," + leave.getContent() + "。");
System.out.println("部门经理审批:同意。");
}
}
package com.xxliao.pattern.behavioral.chain_responsibility.demo;
/**
* @author xxliao
* @description: 具体处理类 - 总经理
* @date 2024/5/27 17:39
*/
public class GeneralManager extends Handler{
public GeneralManager() {
//部门经理处理7天以上的请假
super(Handler.NUM_SEVEN);
}
@Override
protected void handleLeave(LeaveRequest leave) {
System.out.println(leave.getName() + "请假" + leave.getNum() +
"天," + leave.getContent() + "。");
System.out.println("总经理审批:同意。");
}
}
- 客户类角色
package com.xxliao.pattern.behavioral.chain_responsibility.demo;
/**
* @author xxliao
* @description: 请假条实体
* @date 2024/5/27 17:26
*/
public class LeaveRequest {
private String name;//名字
private int num;//请假天数
private String content;//请假内容
public LeaveRequest(String name, int num, String content) {
this.name = name;
this.num = num;
this.content = content;
}
public String getName() {
return name;
}
public int getNum() {
return num;
}
public String getContent() {
return content;
}
}
- 测试客户端
package com.xxliao.pattern.behavioral.chain_responsibility.demo;
/**
* @author xxliao
* @description: 责任链模式 测试客户端
* @date 2024/5/27 17:41
*/
public class Client {
public static void main(String[] args) {
//请假条来一张
LeaveRequest leave = new LeaveRequest("小花",5,"身体不适");
//各位领导
GroupLeader groupLeader = new GroupLeader();
Manager manager = new Manager();
GeneralManager generalManager = new GeneralManager();
groupLeader.setNextHandler(manager);//小组长的领导是部门经理
manager.setNextHandler(generalManager);//部门经理的领导是总经理
//之所以在这里设置上级领导,是因为可以根据实际需求来更改设置,如果实战中上级领导人都是固定的,则可以移到领导实现类中。
//提交申请
groupLeader.submit(leave);
}
}
- 测试结果

行为型模式---责任链模式(chain responsibility)&spm=1001.2101.3001.5002&articleId=139399048&d=1&t=3&u=36813b9249e84979a4e5197cfa76ff4c)
1453

被折叠的 条评论
为什么被折叠?



