责任链模式又名职责链模式,为了避免请求发送者与多个请求处理者耦合在一起,将所有请求的处理者通过前一对象记住其下一个对象的引用而连成一条链;当有请求发生时,可将请求沿着这条链传递,直到有对象处理它为止。
职责链模式主要包含以下角色:
现需要开发一个请假流程控制系统。请假一天以下的假只需要小组长同意即可;请假1天到3天的假还需要部门经理同意;请求3天到7天还需要总经理同意才行。
/**
* @author 晓风残月Lx
* @date 2023/7/27 21:15
* 请假条类
*/
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;
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 21:17
* 抽象处理者
*/
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 numStart;
private int numEnd;
// 声明后继者(声明上级领导)
private Handler nextHandler;
public Handler(int numStart) {
this.numStart = numStart;
}
public Handler(int numStart, int numEnd) {
this.numStart = numStart;
this.numEnd = numEnd;
}
// 设置上级领导对象
public void setNextHandler(Handler nextHandler) {
this.nextHandler = nextHandler;
}
// 各级领导处理请求条的方法
protected abstract void handlerLeave(LeaveRequest leaveRequest);
// 提交请求条
public final void submit(LeaveRequest leaveRequest) {
// 该领导先审批
this.handlerLeave(leaveRequest);
if (this.nextHandler != null && leaveRequest.getNum() > this.numEnd) {
// 提交给上级领导进行审批
this.nextHandler.submit(leaveRequest);
} else {
System.out.println("流程结束!");
}
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 21:23
* 小组长类
*/
public class GroupLeader extends Handler {
public GroupLeader() {
super(0, Handler.NUM_ONE);
}
@Override
protected void handlerLeave(LeaveRequest leaveRequest) {
System.out.println(leaveRequest.getName() + "请假" + leaveRequest.getNum() + "天," + leaveRequest.getContent());
System.out.println("小组长审批:同意");
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 21:23
* 部门经理类
*/
public class ManagerLeader extends Handler {
public ManagerLeader() {
super(Handler.NUM_ONE, Handler.NUM_THREE);
}
@Override
protected void handlerLeave(LeaveRequest leaveRequest) {
System.out.println(leaveRequest.getName() + "请假" + leaveRequest.getNum() + "天," + leaveRequest.getContent());
System.out.println("部门经理审批:同意");
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 21:23
* 总经理类
*/
public class GeneralManagerLeader extends Handler {
public GeneralManagerLeader() {
super(Handler.NUM_THREE, Handler.NUM_SEVEN);
}
@Override
protected void handlerLeave(LeaveRequest leaveRequest) {
System.out.println(leaveRequest.getName() + "请假" + leaveRequest.getNum() + "天," + leaveRequest.getContent());
System.out.println("总经理审批:同意");
}
}
/**
* @author 晓风残月Lx
* @date 2023/7/27 21:28
*/
public class Client {
public static void main(String[] args) {
// 创建一个请求
LeaveRequest leaveRequest = new LeaveRequest("小吕", 1, "感冒了");
LeaveRequest leaveRequest1 = new LeaveRequest("晓风残月", 6, "开摆");
// 创建各级领导
GroupLeader groupLeader = new GroupLeader();
ManagerLeader managerLeader = new ManagerLeader();
GeneralManagerLeader generalManagerLeader = new GeneralManagerLeader();
// 设置处理者链
groupLeader.setNextHandler(managerLeader);
managerLeader.setNextHandler(generalManagerLeader);
// 提交请求
groupLeader.submit(leaveRequest);
groupLeader.submit(leaveRequest1);
}
}
1,优点:
降低了对象之间的耦合度,该模式降低了请求发送者和接收者的耦合度。
增强了系统的可扩展性,可以根据需要增加新的请求处理类,满足开闭原则。
增强了给对象指派职责的灵活性,当工作流程发生变化,可以动态地改变链内的成员或者修改它们的次序,也可动态地新增或者删除责任。
责任链简化了对象之间的连接,一个对象只需保持一个指向其后继者的引用,不需保持其他所有处理者的引用,这避免了使用众多的 if 或者 if···else 语句。
责任分担,每个类只需要处理自己该处理的工作,不能处理的传递给下一个对象完成,明确各类的责任范围,符合类的单一职责原则。
2,缺点:
在javaWeb应用开发中,FilterChain是职责链(过滤器)模式的典型应用,以下是Filter的模拟实现分析:
模拟web请求Request以及web响应Response
public interface Request{
}
public interface Response{
}
模拟web过滤器Filter.
public interface Filter {
public void doFilter(Request req,Response res,FilterChain c);
}
模拟实现具体过滤器
public class FirstFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
System.out.println("过滤器1 前置处理");
// 先执行所有request再倒序执行所有response
chain.doFilter(request, response);
System.out.println("过滤器1 后置处理");
}
}
public class SecondFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
System.out.println("过滤器2 前置处理");
// 先执行所有request再倒序执行所有response
chain.doFilter(request, response);
System.out.println("过滤器2 后置处理");
}
}
模拟实现过滤器链FilterChain
public class FilterChain {
private List<Filter> filters = new ArrayList<Filter>();
private int index = 0;
// 链式调用
public FilterChain addFilter(Filter filter) {
this.filters.add(filter);
return this;
}
public void doFilter(Request request, Response response) {
if (index == filters.size()) {
return;
}
Filter filter = filters.get(index);
index++;
filter.doFilter(request, response, this);
}
}
测试类
public class Client {
public static void main(String[] args) {
Request req = null;
Response res = null ;
FilterChain filterChain = new FilterChain();
filterChain.addFilter(new FirstFilter()).addFilter(new SecondFilter());
filterChain.doFilter(req,res);
}
}