OkHttp3.0拦截器原理——责任链模式

目录

什么是拦截器

拦截器中的类

拦截器中的源码

拦截器源码的逻辑流程


什么是拦截器

拦截器是OkHttp中提供一种强大机制,它可以实现网络监听、请求以及响应重写、请求失败重试等功能。

Okhttp3.0中的拦截器有点像安卓里面的触控反馈的Interceptor。既一个网络请求,按一定的顺序,经由多个拦截器进行处理,该拦截器可以决定自己处理并且返回我的结果,也可以选择向下继续传递,让后面的拦截器处理返回它的结果。这个设计模式叫做责任链模式。

与Android中的触控反馈interceptor的设计略有不同的是,后者通过返回true 或者 false 来决定是否已经拦截。而OkHttp这里的拦截器通过函数调用的方式,讲参数传递给后面的拦截器的方式进行传递。这样做的好处是拦截器的逻辑比较灵活,可以在后面的拦截器处理完并返回结果后仍然执行自己的逻辑;缺点是逻辑没有前者清晰。

拦截器中的类

在拦截器这种结构中,我们只要能掌握好以下两个角色就可以了

    Interceptor                这个我们的拦截器接口,所有拦截器都要实现它

    Chain                         这个是串联起拦截器流程的链

下面来看看这两个类中的逻辑:

1.Chain中持有所有要调用的Interceptor 的列表集合incps

Chain中的proceed方法:调用调用每个Interceptor 的intercept方法 会把Chain自己和去掉第一个Interceptor 的incpts(已经调用完的Interceptor 后面就不调用)作为参数传入

2.Interceptor 中

intercept方法:处理自身负责的逻辑,该方法在Chain中被调用并传入Chain本身,在执行了自身的逻辑之后,调用传入的Chain的proceed

总结就是,Chain中的procced与Interceptor 中的intercept相互循环调用,而procced中每次Chain中的Interceptor 都会往后移动一位,所以这个循环当所以Interceptor 执行过后就会终止。

拦截器中的源码

那我们来看看他们通过源码来看看他们内部是如何做到有序地串联的。

拦截器接口的源码:

public interface Interceptor {
 Response intercept(Chain chain) throws IOException;

 interface Chain {
   Request request();

   Response proceed(Request request) throws IOException;

   Connection connection();
 }
}

其中的Chain就是我们是用来传递的链。这里的传递逻辑伪代码如下:
代码的最外层逻辑(下面这个不只是chain的,是整个的伪代码)

Request request = new Request(){};
 
 
 Arrlist<Interceptor> incpts = new Arrlist();
 Interceptor icpt0 = new Interceptor(){ XXX };
 Interceptor icpt1 = new Interceptor(){ XXX };
 Interceptor icpt2 = new Interceptor(){ XXX };
 ...
 incpts.add(icpt0);
 incpts.add(icpt1);
 incpts.add(icpt2);
 
 Interceptor.Chain chain  = new MyChain(incpts);
 chain.proceed(request);

下面就是最关键的源码部分,基本上所以的责任链Chain都是按着这个模板来的

封装的Chain的内部逻辑

public class MyChain implement Interceptor.Chain{
    Arrlist<Interceptor> incpts;
    int index = 0;
    
    public MyChain(Arrlist<Interceptor> incpts){
        this(incpts, 0);
    }
    
    public MyChain(Arrlist<Interceptor> incpts, int index){
        this.incpts = incpts;
        this.index =index;
    }
    
    public void setInterceptors(Arrlist<Interceptor> incpts ){
        this.incpts = incpts;
    }
 
    @override
    Response proceed(Request request) throws IOException{
            Response response = null;
            ...
            //取出第一个interceptor来处理
            Interceptor incpt = incpts.get(index);
            //生成下一个Chain,index标识当前Interceptor的位置。
            Interceptor.Chain nextChain = new MyChain(incpts,index+1);
            response =  incpt.intercept(nextChain);
            ...
            return response;
    }
  }

而各个Interceptor源码中的实现:

public class MyInterceptor implement Intercetpor{
    @Override 
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //前置拦截逻辑
        ...
        Response response = chain.proceed(request);//传递Interceptor
        //后置拦截逻辑
        ...
        return response;
    }
}

在这个链中,最后的一个Interceptor一般用作生成最后的Response操作,它不会再继续传递给下一个。

拦截器源码的逻辑流程

上面就是整个逻辑的源码了,虽然不多,但是逻辑有点绕(莫慌,看了下面你就会懂了)

这里为了让调用顺序更直观些,直接把源码的调用关系画出

这是Interceptor和Chain的源码以及方法的调用关系,是不是有点像递归函数的感觉。不过两者还是有区别的,递归函数是不断调用自身,而这种拦截器的逻辑是两个类(或者说是类内部的方法)不断相互调用,以某个条件作为终结。

诸如此类的逻辑还有   自定义控件的点击事件拦截,ClassLoader的双亲委派机制,其他的这里就不做展开

关于其中的顺序,用流程图来看更直观  (其中,所有Chain都是同一个,括号内编号表示第几次调用proceed方法)

说了这么多都是源码,这里附上OkHttp中实际各个拦截器的调用顺序,方便大家比照一下

       以上就是拦截器的整个流程,关于拦截器的具体运用,我将在另一篇文章中详细阐述

猜你喜欢

转载自blog.csdn.net/qq_39037047/article/details/84425865
今日推荐