android okhttp 责任链简单分析

来源:互联网 发布:手机修改图片软件 编辑:程序博客网 时间:2024/06/11 09:28

前言

实践是检验真理的唯一标准。没有经过实践形成的blog都是胡扯~

看了 24中设计模式拦截器模式,看了okhttp官方的 wiki 拦截器,还是不懂怎么办!!!!!?

试试 手写一遍试试吧!手写一遍试试吧!手写一遍试试吧!

下面是仿版拦截器,主要验证一些拦截器关键点,这次我要反着来

运行结果:

我是拦截器一1我是拦截器2我是拦截器3我是最后一个拦截器 :在这里不进行向下传递,进行消耗拦截器3响应返回拦截器2响应返回拦截器一响应返回

定义拦截器 Interceptor

public interface Interceptor {    Response intercept(Chain chain) throws IOException;    interface Chain {        Requestx request();        Response proceed(Requestx request) throws IOException;        /**         * Returns the connection the request will be executed on. This is only         * available in the chains of network interceptors; for application         * interceptors this is always null.         */        @Nullable        Connection connection();    }}

定义具体拦截器实现类

public class RealInterceptorChain implements Interceptor.Chain {    private final List<Interceptor> interceptors;    public int index;    private final Requestx request;    private int calls;    public RealInterceptorChain(List<Interceptor> interceptors, int index,            Requestx request) {        this.interceptors = interceptors;        this.index = index;        this.request = request;    }    @Override    public Requestx request() {        return request;    }    @Override    public Response proceed(Requestx request) throws IOException {        if (index >= interceptors.size())            throw new AssertionError();        calls++;        RealInterceptorChain next = new RealInterceptorChain(interceptors,                index + 1, request);        Interceptor interceptor = interceptors.get(index);        Response response = interceptor.intercept(next);        if (index + 1 < interceptors.size() && next.calls != 1) {            throw new IllegalStateException("network interceptor "                    + interceptor + " must call proceed() exactly once");        }        return response;    }    @Override    @Nullable    public Connection connection() {        return new Connection();    }}

定义的Response ,RequestX,Connection

public final class Response {    public Response() {        super();    }}
public class Requestx {    public Requestx() {    }}
public class Connection {}

定义拦截器 InterceptorOne,InterceptorTwo,InterceptorThress,LastInterceptor

/** * 1.传递下去 条件 2.消耗在当前类 *  * @author weichyang *  */public class InterceptorOne implements Interceptor {    @Override    public Response intercept(Chain chain) throws IOException {        RealInterceptorChain chain2 = (RealInterceptorChain) chain;        System.out.println("我是拦截器一" + chain2.index);        Requestx requestx = chain.request();        Response response = chain.proceed(requestx); // 调用实现进入拦截器二        System.out.println("拦截器一响应返回");        return response;    }}
public class InterceptorTwo implements Interceptor {    @Override    public Response intercept(Chain chain) throws IOException {        System.out.println("我是拦截器2");        Requestx requestx = chain.request();        Response response = chain.proceed(requestx);        System.out.println("拦截器2响应返回");        return response;    }}
public class InterceptorThree implements Interceptor {    @Override    public Response intercept(Chain chain) throws IOException {        System.out.println("我是拦截器3");        Requestx requestx = chain.request();        Response response = chain.proceed(requestx);        System.out.println("拦截器3响应返回");        return response;    }}
public class LastInterceptor implements Interceptor {    @Override    public Response intercept(Chain chain) throws IOException {        System.out.println("我是最后一个拦截器 :在这里不进行向下传递,进行消耗");        return new Response();    }}

定义拦截器调用入口

public class Client {    public static void main(String[] args) {        List<Interceptor> interceptors = new ArrayList();        interceptors.add(new InterceptorOne());        interceptors.add(new InterceptorTwo());        interceptors.add(new InterceptorThree());        interceptors.add(new LastInterceptor());        Requestx request = new Requestx();        RealInterceptorChain realInterceptorChain = new RealInterceptorChain(                interceptors, 0, request);        try {            realInterceptorChain.proceed(request);        } catch (IOException e) {            e.printStackTrace();        }    }}

以上代码就是简化版okhttp 拦截器。

重复责任链设计模式关键点:

1.待处理问题 (request,respond)
2.责任链传递顺序 (chain.proceed(requestx))
3.边界问题,最终处理 (LastInterceptor.class 当然 前面的链式中也可以控制是否向下传递,不去调用chain.proceed(requestx))

注意: 责任设计模式顺序调用,依顺序进行处理。没的说,看着就明白,关键点在阻塞式的响应。

我是拦截器一1
我是拦截器2
我是拦截器3
我是最后一个拦截器 :在这里不进行向下传递,进行消耗
拦截器3响应返回
拦截器2响应返回
拦截器一响应返回

拦截器调用链

这里写图片描述

到这就没有了……,是不是没看懂。 回头再看一遍

原创粉丝点击