设计模式之责任链模式

Chain of responsibility

责任链模式的概念、责任链模式的结构、责任链模式的优缺点、责任链模式的使用场景、责任链模式的实现示例、责任链模式的源码分析


1、责任链模式的概念

  责任链模式,即把请求从链中的一个对象传到下一个对象,知道请求被响应为止,通过这种方式来解耦。为了避免请求发送者与多个请求处理者耦合,通过前一个处理者对下一个处理者的引用而形成请求处理链,当请求发生时将沿着链向下传递,知道被处理为止。

2、责任链模式的结构

  • 抽象处理器:定义请求处理行为,并持有维护一个下一个处理器的引用。
  • 具体处理器:继承自抽象处理器,实现其定义的请求处理行为。
  • 客户端:即请求发起者,创建链并发起请求。

chain-of-responsibility

3、责任链模式的优缺点

  • 优点:
    • 降低了对象之间的耦合度。降低了请求发送者和接受者之间的耦合度。
    • 增强了系统的可扩展性。可以根据需求增加新的请求处理类,满足开闭原则。
    • 增强了给对象指派职责的灵活度。当工作流程发生变化时,可动态的改变链内成员或修改它们的次序,也可动态的新增或删除责任。
    • 责任链简化了对象之间的连接。一个对象只需要持有一个后继引用,不需要持有其它处理者的引用。
    • 责任分担。每个类只需要负责自己该处理的工作,不能处理的传递给下一个对象,明确各类的责任范围,符合类的单一职责原则。
  • 缺点:
    • 不能保证每个请求一定会被处理。由于一个请求没有明确的接受者,所以不能保证它一定会被处理,该请求可能一直传至链尾都没被处理。
    • 职责链的合理性需要客户端来保证,增加了客户端的复杂性,也可能由于错误的职责链设置而导致系统出错。

4、责任链模式的使用场景

  • 多个对象可处理同一个请求,但具体有那个对象处理则在运行时动态决定。
  • 在请求处理者不明确的情况下想多个对象提交一个请求。
  • 需要动态处理一组对象处理请求时。

5、责任链模式的实现示例

抽象处理器:

public abstract class Handler {

    protected static final Integer ZERO = 0;

    protected static final Integer ONE = 1;

    protected static final Integer THREE = 3;

    protected static final Integer SEVEN = 7;

    private Integer start;

    private Integer end;

    private Handler next;

    public Handler(Integer start, Integer end) {
        this.start = start;
        this.end = end;
    }

    public void setNext(Handler next) {
        this.next = next;
    }

    /**
     * 提交
     * @param leaveOfRequest
     */
    public final void submit(LeaveOfRequest leaveOfRequest) {
        int days = leaveOfRequest.getDays();
        if (days <= 0) {
            return;
        }

        if (days >= this.start) {
            this.handler(leaveOfRequest);
            if (this.next != null && days > this.end) {
                this.next.submit(leaveOfRequest);
            } else {
                System.out.println("流程结束");
            }
        }
    }

    /**
     * 处理请求
     * @param leaveOfRequest
     */
    protected abstract void handler(LeaveOfRequest leaveOfRequest);
}

员工处理器:

public class EmployeeHandler extends Handler {

    public EmployeeHandler() {
        super(ZERO, ZERO);
    }

    @Override
    protected void handler(LeaveOfRequest leaveOfRequest) {
        System.out.println("请假条:" + leaveOfRequest.getName() + " 请假 " + leaveOfRequest.getDays() + " 天 原因 "
                + leaveOfRequest.getContent());
    }
}

组长处理器:

public class GroupHandler extends Handler {

    public GroupHandler() {
        super(ZERO, ONE);
    }

    @Override
    protected void handler(LeaveOfRequest leaveOfRequest) {
        System.out.println("组长同意");
    }
}

部门经理处理器:

public class ManagerHandler extends Handler {

    public ManagerHandler() {
        super(ONE, THREE);
    }

    @Override
    protected void handler(LeaveOfRequest leaveOfRequest) {
        System.out.println("部门经理同意");
    }
}

总经理处理器:

public class GeneralHandler extends Handler {

    public GeneralHandler() {
        super(THREE, SEVEN);
    }

    @Override
    protected void handler(LeaveOfRequest leaveOfRequest) {
        System.out.println("总经理同意");
    }
}

请假条(不属于责任链模式组件):

public class LeaveOfRequest {

    private String name;

    private Integer days;

    private String content;

    public LeaveOfRequest(String name, Integer days, String content) {
        this.name = name;
        this.days = days;
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getDays() {
        return days;
    }

    public void setDays(Integer days) {
        this.days = days;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

测试:

public class ChainOfResponsibilityTest {

    public static void main(String[] args) {
        LeaveOfRequest leaveOfRequest = new LeaveOfRequest("momo", 5, "调休");

        EmployeeHandler employeeHandler = new EmployeeHandler();
        GroupHandler groupHandler = new GroupHandler();
        ManagerHandler managerHandler = new ManagerHandler();
        GeneralHandler generalHandler = new GeneralHandler();
        employeeHandler.setNext(groupHandler);
        groupHandler.setNext(managerHandler);
        managerHandler.setNext(generalHandler);

        employeeHandler.submit(leaveOfRequest);
    }
}

测试结果:

请假条:momo 请假 5 天 原因 调休
组长同意
部门经理同意
总经理同意
流程结束

6、责任链模式源码分析

  java web 中的 FilterChain 就是责任链模式的典型应用,spring security 中的 SecurityFilterChain 也是责任链模式的典型应用,其在 FilterOrderRegistration 类中声明了默认内置 filter 的顺序,并在 HttpSecurity 类中声明了内部类 OrderedFilter 来承载 Filter,并对外提供了可以指定顺序的 addFilter 方法,以这些手段来明确 filter 在 filter chain 中的执行顺序。

  以下为 FilterChain 实现示例:

chain-of-responsibility-filter-chain

模拟 Request 类:

public interface Request {

}

模拟 Response 类:

public interface Response {

}

模拟 Filter 类:

public interface Filter {

    void doFilter(Request request, Response response, FilterChain filterChain);
}

模拟 FilterChain 类:

public class FilterChain {

    private List<Filter> filters;

    private Integer index;

    public FilterChain() {
        this.filters = new ArrayList<>();
        this.index = 0;
    }

    public FilterChain addFilter(Filter filter) {
        this.filters.add(filter);
        return this;
    }

    public void doFilter(Request request, Response response) {
        if (this.index == this.filters.size()) {
            return;
        }
        Filter filter = this.filters.get(index);
        this.index++;
        filter.doFilter(request, response, this);
    }
}

自定义过滤器一:

public class FirstFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {
        System.out.println("过滤器一执行前");
        filterChain.doFilter(request, response);
        System.out.println("过滤器一执行后");
    }
}

自定义过滤器二:

public class SecondFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {
        System.out.println("过滤器二执行前");
        filterChain.doFilter(request, response);
        System.out.println("过滤器二执行后");
    }
}

自定义过滤器三:

public class ThirdFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {
        System.out.println("过滤器三执行前");
        filterChain.doFilter(request, response);
        System.out.println("过滤器三执行后");
    }
}

测试:

public class FilterTest {

    public static void main(String[] args) {
        FilterChain filterChain = new FilterChain();
        filterChain.addFilter(new FirstFilter())
                .addFilter(new SecondFilter())
                .addFilter(new ThirdFilter());
        filterChain.doFilter(null, null);
    }
}

测试结果:

过滤器一执行前
过滤器二执行前
过滤器三执行前
过滤器三执行后
过滤器二执行后
过滤器一执行后
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值