`添加自定义、cookie等等的拦截器,今天我们主要看看后半部分:
```
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
```
首先初始化了 `RealInterceptorChain`,`RealInterceptorChain`是`Interceptor.Chain`的实现类

先看一下`Interceptor.Chain`:
```
public interface Interceptor {
Response intercept(Chain chain) throws IOException;
interface Chain {
Request request();
Response proceed(Request request) throws IOException;
//部分代码省略....
}
}
```
生成了RealInterceptorChain的实例,调用了`proceed()`,返回了最后的Response
我们看下 `RealInterceptorChain`类中的`proceed()`:
```
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
if (index >= interceptors.size()) throw new AssertionError();
calls++;
// If we already have a stream, confirm that the incoming request will use it.
if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
+ " must retain the same host and port");
}
// If we already have a stream, confirm that this is the only call to chain.proceed().
if (this.httpCodec != null && calls > 1) {
throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
+ " must call proceed() exactly once");
}
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
// Confirm that the next interceptor made its required call to chain.proceed().
if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
throw new IllegalStateException("network interceptor " + interceptor
+ " must call proceed() exactly once");
}
// Confirm that the intercepted response isn‘t null.
if (response == null) {
throw new NullPointerException("interceptor " + interceptor + " returned null");
}
if (response.body() == null) {
throw new IllegalStateException(
"interceptor " + interceptor + " returned a response with no body");
}
return response;
}
```
经过一系列的判断,看下`proceed()`的核心
```
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
```
如果对责任链模式有认识的朋友看到上面代码,应该直接就能看出来了,**并能分析出:**
- 如果是责任链模式, 那个`intercept()`一定是关键, `Interceptor`是接口,interceptors集合中的拦截器类肯定都实现了 `Interceptor`以及 `interceptor.intercept()`
- 每个拦截器 `intercept() `方法中的chain,都在上一个 `chain`实例的 `chain.proceed() `中被初始化,并传递了拦截器List与 `index `,直接最后一个 `chain实例 `执行即停止。

### 总结:
每个`chain`的`interceptors`与`index`都是由上一个`chain`初始化传递过来的,在`chain.proceed()`中获取对应的`interceptor实例` 并初始化下一个`chain`,直到最后一个`chain`被执行。
这样就清晰了吧?责任链模式的重点在“链上”,由一条链去处理相似的请求,在链中决定谁来处理这个请求,并返回相应的结果。

这节就说到这,希望对大家有所帮助.....
大家可以关注我的微信公众号:「秦子帅」一个有质量、有态度的公众号!
Okhttp3源码解析(4)-拦截器与设计模式
原文:https://www.cnblogs.com/qinzishuai/p/11416058.html