OkHttp源码责任链解析

2019-12-21  本文已影响0人  云木杉

责任链模式的运用

public interface Interceptor {

  Response intercept(Chain chain) throws IOException;

  interface Chain {
    Request request();
    Response proceed(Request request) throws IOException;
  }
}
    Interceptor.Chain chain = new RealInterceptorChain(interceptors);
    chain.proceed(....,index = 0)
 public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,int index) {
      
    // 这里传入的拦截器,角标加1 
    RealInterceptorChain next = new RealInterceptorChain(interceptors[index + 1], request);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);

    return response;
  }
@Override 
public Response intercept(Chain chain) {

    Reponse networkResponse = chain.proceed(networkRequest);

    return networkResponse;

}
上一篇 下一篇

猜你喜欢

热点阅读