责任链模式(Chain of Responsibility)是一种处理请求的模式,它让多个处理器都有机会处理该请求,直到其中某个处理成功为止。
责任链模式解决的问题:
避免请求的发送者和接收者之间的耦合关系。
Rxjava的拦截器是典型的责任链例子,同时我们自己实现类似的拦截器功能时,也会用到责任链模式,通过研究Rxjava的源码,总结两种实现方案:
- 方案一:List,for循环+返回值(最简单的实现)
- 方案二:List,链式+手动调用(Rxjava的实现)
方案一:for循环+返回值
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public interface Interceptor { Boolean intercept(Request request); }
public class InterceptorA implements Interceptor { public Boolean intercept(Request request){ ...
return true return false } }
public class InterceptorChain {
private List<Interceptor> interceptors = new ArrayList<Interceptor>();
public void addInterceptor(Interceptor interceptor){ this.interceptors.add(interceptor); }
public boolean process(Request request){ for(Interceptor interceptor : interceptors) { Boolean result = interceptor.intercept(request); if(result == true) { return ; } } }
public static void main(String[] args) { InterceptorChain chain = InterceptorChain(); chain.addInterceptor(new InterceptorA()); chain.addInterceptor(new InterceptorB());
boolean result = chain.process(new Request("Hello Wrold")); } }
|
类图
时序图
方案二:链式+手动调用
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public interface Interceptor { Response intercept(Chain chain, Request request); }
public interface Chain { Response process(Request request); }
public class InterceptorA implements Interceptor { Response intercept(Chain chain, Request request){ ...
return null; return chain.process(request); } }
public class InterceptorChain implements Chain {
private final List<Interceptor> interceptors = new ArrayList<Interceptor>(); private final int index;
public InterceptorChain(List<Interceptor> interceptors, int index){ this.interceptors = interceptors; this.index = index; }
@Override public Response process(Request request); if(index >= interceptors.size()) { return processFinal(request); }
InterceptorChain next = new InterceptorChain(interceptors, index + 1); Interceptor interceptor = interceptors.get(index); return interceptor.intercept(next, request); }
public static void main(String[] args) { List<Interceptor> interceptors = new ArrayList<Interceptor>(); interceptors.add(new InterceptorA()); interceptors.add(new InterceptorB());
Response response = new InterceptorChain(interceptors, 0).process(new Request("Hello Wrold")); } }
|
类图
时序图
对比
两种方案的最大差别在于:方案二可以实现Request对象处理前的拦截,和处理后的拦截,原理是通过函数的递归调用。
参考
- 廖雪峰-责任链
- 类图+时序图参考