责任链模式

责任链模式

定义:Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.(使多个对象都有机会处理请求,从而避免了请求的发送者接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有对象处理它为止。)

责任链模式的通用类图如下图:
img "责任链模式类图")

责任链模式的应用

1.责任链模式的优点

责任链模式非常显著的优点是将请求和处理分开。请求者可以不用知道谁处理的,处理者可以不用知道请求的全貌,两者解耦,提高系统的灵活性。

2.责任链模式的缺点

责任链模式有两个非常显著的缺点:一是性能问题,每个请求都是从链头到链尾,特别是在链比较长的时候,性能是一个非常大的问题。二是调试不很方便,特别是链条比较长,环节比较多的时候,由于采用了类似递归的方式,调试的时候逻辑可能比较复杂。

3.责任链模式的注意事项

链中节点数量需要控制,避免出现超长链的情况,一般的做法是在Handler中设置一个最大节点数量,在SetNext方法中判断是否是否是超过其阀值,超过则不允许该链建立,避免无意识地破坏系统性能。

代码示例:

CORTest

public class CORTest {

    public static void main(String[] args) {
        FilterChain chain = new FilterChainOne();
        chain.addFilter(new FilterOne());
        chain.addFilter(new FilterTwo());
        chain.addFilter(new FilterThree());
        chain.doFilter(new HTRequest(), new HTResponse());
    }
}

Filter

public interface Filter {

    void doFilter(HTRequest request, HTResponse response, FilterChain chain);
}

FilterChain

public interface FilterChain {
    
    void doFilter(HTRequest request, HTResponse response);
    
    void addFilter(Filter filter);
}

FilterChainOne


public class FilterChainOne implements FilterChain {

    public List<Filter> filters;
    private int index;

    public FilterChainOne() {
        init();
    }
    
    private void init() {
        this.filters = new ArrayList<>();
        this.index = 0;
    }

    @Override
    public void doFilter(HTRequest request, HTResponse response) {
        if (filters == null || index == filters.size()) {
            return;
        }
        filters.get(index++).doFilter(request, response, this);
    }

    @Override
    public void addFilter(Filter filter) {
        this.filters.add(filter);
    }
}

FilterOne

public class FilterOne implements Filter {

    @Override
    public void doFilter(HTRequest request, HTResponse response, FilterChain chain) {
        System.out.println("FilterOne.doFilter: before");
        chain.doFilter(request, response);
        System.out.println("FilterOne.doFilter: after");
    }
}

FilterThree

public class FilterThree implements Filter{
    
    @Override
    public void doFilter(HTRequest request, HTResponse response, FilterChain chain) {
        System.out.println("FilterThree.doFilter: before");
        chain.doFilter(request, response);
        System.out.println("FilterThree.doFilter: after");
    }
}

FilterTwo

public class FilterTwo implements Filter{
    
    @Override
    public void doFilter(HTRequest request, HTResponse response, FilterChain chain) {
        System.out.println("FilterTwo.doFilter: before");
        chain.doFilter(request, response);
        System.out.println("FilterTwo.doFilter: after");
    }
}

HTRequest

public class HTRequest {
}

HTResponse

public class HTResponse {
}

运行结果:

FilterOne.doFilter: before
FilterTwo.doFilter: before
FilterThree.doFilter: before
FilterThree.doFilter: after
FilterTwo.doFilter: after
FilterOne.doFilter: after
posted @ 2023-04-24 16:39  我见青山应如是  阅读(15)  评论(0编辑  收藏  举报