浅谈设计模式——责任链模式(OKHttp中的责任链模式)

一、 什么是责任链模式

责任链, 顾名思义是将多个节点通过链条的方式连接起来,每一个节点相当于一个对象,而每一个对象层层相关,直接或者间接引用下一个对象(节点);直到链条中有一个节点处理头节点传下来的事件截止。

二、责任链模式使用场景(以下摘自 Android源码设计模式)

有一事件,可以被多个对象同时处理,但是由哪个对象处理则在运行时动态决定!
在请求处理者不明确时向多个对象中提交一个请求。
动态指定一组对象处理请求

三、责任链模式UML图

责任链模式.png

客户端发出请求,调用抽象类Handler中的方法处理逻辑业务。对象ConcreteHandler1与ConcreteHandler2继承Handler,其中ConcreteHandler1中持有下一个节点ConcreteHandler2的引用;事件由1对象发出,如果其处理不了,则交由2对象处理! 这是简单的责任链模式结构图,下面使用代码的方式展现:

Handler.class

/**
 * 抽象类
 */
public abstract class Handler {

    /**
     * 下一代处理者
     */
    public Handler nextProcessor;

    /**
     * 每一个实现类处理
     * 
     * @param msg
     */
    public abstract void handleRequest(String msg);
    
}

Processor1.class


/**
 * 处理者1
 */
public class Processor1 extends Handler {
    @Override
    public void handleRequest(String msg) {
        if(msg.equals("Processor1")) {
            System.out.println("第一个处理者处理");
        } else {
            nextProcessor.handleRequest(msg);
        }
    }
}

Processor2.class


/**
 * 处理者2
 */
public class Processor2 extends Handler {
    @Override
    public void handleRequest(String msg) {
        if(msg.equals("Processor2")) {
            System.out.println("第二个处理者处理");
        } else {
            nextProcessor.handleRequest(msg);
        }
    }
}

测试方法:

@Test
public void testProcessor() {
    Processor1 processor1 = new Processor1();
    Processor2 processor2 = new Processor2();

    processor1.nextProcessor = processor2;
    processor2.nextProcessor = processor1;

    processor1.handleRequest("Processor2");
    }

// 运行结果:
第二个处理者处理
Process finished with exit code 0

四、OKHttp中的责任链模式

<font color="red" size=1> 摘自百度百科</font>
android网络框架之OKhttp
一个处理网络请求的开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso)
用于替代HttpUrlConnection和Apache HttpClient(android API23 6.0里已移除HttpClient,现在已经打不出来)

在使用OKHttp之前,我们可能用到更多的网络请求是Async-Http,一种用于异步处理网络的框架,或者更加直接的是使用android自带的HttpUrlConnection 和 HttpClient ,对其进行简单的封装; OKHttp开源出之后,几乎大部分项目都使用到这个开源框架,它有如下有点:

1. 官方在6.0以后添加了OKHttp
2. okHttp支持SPDY

// 同时能进行的最大请求数
private int maxRequests = 64;
// 同时请求的相同HOST的最大个数
private int maxRequestsPerHost = 5;

okhttp3.Dispatcher.class 中定义了这两个变量,并发数可以支持到64,当然这两个数值是可以自定义的,这说明OKHttp是支持SPDY的(<font color="#666666" size=1>谷歌开发的基于TCP的应用层协议,用于最小化网络延迟,提升网络速度,优化用户的网络使用体验. SPDY并不是一种替代http的协议,只是对http的一种增强</font>)

—— OKHttp的使用 同步获取

private final OkHttpClient client = new OkHttpClient();
 
public void run() throws Exception {
    // 创建Request
    Request request = new Request.Builder()
        .url("http://www.baidu.com/")
        .build();
 
    // 获取到结果
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    System.out.println(response.body().string());
}
—— 异步获取
private final OkHttpClient client = new OkHttpClient();
 
public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://www.baidu.com/")
        .build();
 
    client.newCall(request).enqueue(new Callback() {
      @Override public void onFailure(Request request, Throwable throwable) {
        throwable.printStackTrace();
      }
 
      @Override public void onResponse(Response response) throws IOException {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
 
        Headers responseHeaders = response.headers();
        System.out.println(response.body().string());
      }
    });
}

使用过程很简单,创建一个OKHttpClient, 创建Request对象,使用同步方法顺序获取或者使用回调CallBack方法异步获取数据;执行的方法主要是client中newCall方法和enqueue方法,

——下面我们分析其中的源码:
/**
* Prepares the {@code request} to be executed at some point in the future.
*/
@Override
public Call newCall(Request request) {
   return RealCall.newRealCall(this, request, false /* for web socket */);
}

newCall 方法需要传一个Request,Request对象使用了构建者模式将请求方法,请求体,请求头进行了封装; newCall 获取到了Call 这个接口:

public interface Call extends Cloneable {
  /** 获取到最开始的request */
  Request request();

  /** 执行请求,获取到Response */
  Response execute() throws IOException;

  void enqueue(Callback responseCallback);

  void cancel();

  boolean isExecuted();

  boolean isCanceled();

  Call clone();

  interface Factory {
    Call newCall(Request request);
  }
}

而这个接口的 实现类只有 <font color="red">okhttp3.RealCall.class</font> ,接下来我们看下他的excute() 方法:


  @Override public Response execute() throws IOException {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    eventListener.callStart(this);
    try {
      // 将本次请求添加到事件调度器中
      client.dispatcher().executed(this);
        
      // 今天的主角, 责任链获取到Response结果
      Response result = getResponseWithInterceptorChain();
      if (result == null) throw new IOException("Canceled");
      return result;
    } catch (IOException e) {
      eventListener.callFailed(this, e);
      throw e;
    } finally {
      client.dispatcher().finished(this);
    }
  }
—— Dispatcher

在讲解责任链之前,我们先看下Dispatcher调度器中有些什么?
[图片上传失败...(image-10e627-1536636467207)]

可以知道,它有三个双端队列,

// 双端队列,支持首尾两端 双向开口可进可出
    
    /**
     * 准备运行的异步队列
     * 
     */
    private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();

    // 正在运行的异步
    private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

    // 正在执行的同步队列
    private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();

队列中线程使用线程池:

    /**
     * 线程池的方式启动线程,使用懒加载的方式
     */
    private @Nullable ExecutorService executorService;
    
    public synchronized ExecutorService executorService() {
        if (executorService == null) {
            //TODO 线程池
            //TODO 核心线程 最大线程 非核心线程闲置60秒回收 任务队列
            executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher",
                    false));
        }
        return executorService;
    }

而上面client执行调度器中的excute方法,实际上就是将当前请求直接添加到这个同步的双端队列中,等待线程池中的队列被执行!

—— getResponseWithInterceptorChain()

接下来就要执行拦截器了,而拦截器中就是使用了我们今天所知道的责任链模式,上面的责任链模式已经说的很清晰了,一环接着一环,一个对象持有下个对象的引用;我们看OKHttp中的责任链模式是怎样写的,点击进入该方法:

  Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    //责任链 实际上是像递归一样倒叙执行
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    //5、重试与重定向
    interceptors.add(retryAndFollowUpInterceptor);
    // 4、请求头等信息
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    //3、缓存配置 根据条件(存在响应缓存并被设置为不变的或者响应在有效期内)返回缓存响应设置请求头(If-None-Match、If-Modified-Since等) 服务器可能返回304(未修改)
    interceptors.add(new CacheInterceptor(client.internalCache()));
    //2、连接
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    //1、流操作(写出请求体、获得响应数据)进行http请求报文的封装与请求报文的解析
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
        originalRequest, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
  }

可以看到,getResponseWithInterceptorChain() 方法,是将5个拦截器添加到链表中,再新建了一个RealInterceptorChain.class 类,然后执行了我们责任链中抽象处理类的处理方法 proceed,这里是使用了接口的形式:

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

  interface Chain {
    Request request();

    Response proceed(Request 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();

    Call call();

    int connectTimeoutMillis();

    Chain withConnectTimeout(int timeout, TimeUnit unit);

    int readTimeoutMillis();

    Chain withReadTimeout(int timeout, TimeUnit unit);

    int writeTimeoutMillis();

    Chain withWriteTimeout(int timeout, TimeUnit unit);
  }
}

所以,责任现在都交给了RealInterceptorChain, 上面直接调用了Interceptor.Chain接口中的 proceed方法,我们看下他的实现:

 public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {
      
      .....

    // 创建新的拦截链,链中的拦截器集合index+1
    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
        connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
        writeTimeout);
    //执行当前的拦截器 默认是:retryAndFollowUpInterceptor
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);

    ......
    
    return response;
  }

主要是上面三行代码,首先拿到下一个 拦截器,上面添加的第一个拦截器是 retryAndFollowUpInterceptor (重试与重定向)拦截器,然后将下一个拦截器传入到重试与重定向拦截器中,看看intercept这个方法在实现类中做的操作:

@Override public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();
    // 首先拿到当前真实的Interceptor 实现类
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Call call = realChain.call();
    EventListener eventListener = realChain.eventListener();
    // 核心 协调连接、请求/响应以及复用
    StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
        createAddress(request.url()), call, eventListener, callStackTrace);
    this.streamAllocation = streamAllocation;

    int followUpCount = 0;
    Response priorResponse = null;
    while (true) {
      if (canceled) {
        streamAllocation.release();
        throw new IOException("Canceled");
      }

      Response response;
      boolean releaseConnection = true;
      try {
        //执行到一半,又去执行了RealInterceptorChain中的proceed方法
        //实际上就是下一个拦截器
        response = realChain.proceed(request, streamAllocation, null, null);
        releaseConnection = false;
      } catch (RouteException e) {
        .....
      }
   }

这个过程其实就是递归的过程,而底就是CallServerInterceptor ,这里不对拦截器作详细的讲解,每个拦截器做的处理逻辑都差不多,下面我们看下这个过程的图解:


OKHttp中的责任链.jpg
总结:

OKHttp中使用拦截器对不同的业务进行区分,我们也可以使用自己的自定义拦截器
其中的责任链模式其实和我们设计模式中的有区别,这里是将分发处理给了接口,让其去处理

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容

  • 设计模式概述 在学习面向对象七大设计原则时需要注意以下几点:a) 高内聚、低耦合和单一职能的“冲突”实际上,这两者...
    彦帧阅读 3,710评论 0 14
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,103评论 18 139
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,484评论 2 59
  • 大葱、小葱、白洋葱、红洋葱,都是厨房必备,作用却天差地别,而它们的区别你清楚么。 烹饪方法 大葱 大葱别名和事草,...
    每日养生妙招阅读 2,245评论 0 0
  • 上一章 第三十二章 韩秋香屈辱告知强与朝 仨兄弟商议摆治吕二蛋 吕二蛋走了以后,韩秋香并没有象...
    林木成荫阅读 349评论 2 6