Android 2分钟手写okhttp拦截器 彻底搞懂责任链模式 责任链模式实战demo

有同学说看不懂责任链模式,那么我用100行代码手写拦截器,让你搞懂!!!!

请问:怎么可以把代码格式调整?我贴出来换格式了。你们直接去Github下载工程!



public class Request {

public Request() {

}

}

public class Response {

public Response() {

super();

    }

}

public class Connection {

}

拦截器接口设计

public interface Interceptor {

Responseintercept(Chain chain)throws IOException;//拦截器拦截方法,都有

    interface Chain {//链

        Requestrequest();

        //这个方法比较重要,把它回调回去了,重复执行了proceed方法

        Responseproceed(Request request)throws IOException;

        @Nullable

        Connectionconnection();

    }

}

最重要的类:

public class RealInterceptorChainimplements Interceptor.Chain {

private final Listinterceptors;

    public int index;

    private final Requestrequest;

    private int calls;

    public RealInterceptorChain(List interceptors, int index,

                                Request request) {

this.interceptors = interceptors;

        this.index = index;

        this.request = request;

    }

@Override

    public Requestrequest() {

return request;

    }

//从这里出发

    @Override

    public Responseproceed(Request 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);//调用拦截器的intercepter方法,分别到不同拦截器

        //得到最后一个拦截器的response。

        if (index +1

throw new IllegalStateException("network interceptor "

                    + interceptor +" must call proceed() exactly once");

        }

return response;//又跳到了CacheInterceptor的响应处理

    }

@Override

@Nullable

    public Connectionconnection() {

return new Connection();

    }

}

重试拦截器:

public class RetryAndFollowUpInterceptorimplements Interceptor{

@Override

    public Responseintercept(Chain chain)throws IOException {

RealInterceptorChain chain2 = (RealInterceptorChain) chain;

        System.out.println("我是拦截器一  RetryAndFollowUpInterceptor" + chain2.index);

        Request request = chain.request();

        Response response = chain.proceed(request); // 调用实现进入拦截器二,到了这里不会往下执行,而是执行下一个拦截器

        System.out.println("拦截器一 RetryAndFollowUpInterceptor  响应返回");

        return response;

    }

}

桥接拦截器:

public class BridgeInterceptorimplements Interceptor {

@Override

    public Responseintercept(Chain chain)throws IOException {

System.out.println("我是拦截器2  BridgeInterceptor");

        Request request = chain.request();

        Response response = chain.proceed(request);//这里会分出去

        System.out.println("拦截器2响应返回  BridgeInterceptor");

        return response;

    }

}

缓存拦截器

public class CacheInterceptorimplements Interceptor {

@Override

    public Responseintercept(Chain chain)throws IOException {

System.out.println("我是拦截器3  CacheInterceptor");

        Request request = chain.request();

        Response response = chain.proceed(request);

        System.out.println("拦截器3响应返回  CacheInterceptor");////这里会分出去

        return response;

    }

}

服务器请求拦截器。最特殊,

public class ConnectInterceptorimplements Interceptor {

@Override

    public Responseintercept(Chain chain)throws IOException {

System.out.println("我是最后一个拦截器 :在这里不进行向下传递,进行消耗  ConnectInterceptor");

        return new Response();//最后一个得到相应

    }

}

1.为什么请求是依次的?

上一个的request引用,传递到了下一个request

public Responseproceed(Request request)throws IOException {

2.为什么后面的response先执行?

因为责任链模式

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 我是拦截器一  RetryAndFollowUpInterceptor1

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 我是拦截器2  BridgeInterceptor

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 我是拦截器3   CacheInterceptor

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 我是最后一个拦截器 :在这里不进行向下传递,进行消耗   ConnectInterceptor

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 拦截器3响应返回  CacheInterceptor

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 拦截器2响应返回  BridgeInterceptor

2021-06-16 23:19:34.498 25691-25691/com.glide.myapplication.myapplication I/System.out: 拦截器一 RetryAndFollowUpInterceptor   响应返回


demo地址:https://github.com/pengcaihua123456/shennandadao

你可能感兴趣的:(Android 2分钟手写okhttp拦截器 彻底搞懂责任链模式 责任链模式实战demo)