okhttp中的Builder模式-创新互联

代码

我们以Request类为例,以下是部分代码:okhttp中的Builder模
式
public final class Request {
  private final HttpUrl url;
  private final String method;
  private final Headers headers;
  private final RequestBody body;
  private final Object tag;

  private volatile CacheControl cacheControl; // Lazily initialized.

  private Request(Builder builder) {
    this.url = builder.url;
    this.method = builder.method;
    this.headers = builder.headers.build();
    this.body = builder.body;
    this.tag = builder.tag != null ? builder.tag : this;
  }

  public static class Builder {
    private HttpUrl url;
    private String method;
    private Headers.Builder headers;
    private RequestBody body;
    private Object tag;

    public Builder() {
      this.method = "GET";
      this.headers = new Headers.Builder();
    }

    public Builder url(String url) {
      if (url == null) throw new NullPointerException("url == null");

      // Silently replace websocket URLs with HTTP URLs.
      if (url.regionMatches(true, 0, "ws:", 0, 3)) {
        url = "http:" + url.substring(3);
      } else if (url.regionMatches(true, 0, "wss:", 0, 4)) {
        url = "https:" + url.substring(4);
      }

      HttpUrl parsed = HttpUrl.parse(url);
      if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
      return url(parsed);
    }

    public Builder get() {
      return method("GET", null);
    }

    public Builder method(String method, RequestBody body) {
      if (method == null) throw new NullPointerException("method == null");
      if (method.length() == 0) throw new IllegalArgumentException("method.length() == 0");
      if (body != null && !HttpMethod.permitsRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must not have a request body.");
      }
      if (body == null && HttpMethod.requiresRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must have a request body.");
      }
      this.method = method;
      this.body = body;
      return this;
    }

    public Request build() {
      if (url == null) throw new IllegalStateException("url == null");
      return new Request(this);
    }
  }
}

source:https://github.com/square/okhttp/blob/parent-3.3.1/okhttp/src/main/java/okhttp3/Request.java

成都创新互联网站建设提供从项目策划、软件开发,软件安全维护、网站优化(SEO)、网站分析、效果评估等整套的建站服务,主营业务为成都做网站、成都网站建设,重庆App定制开发以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。成都创新互联深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

说明

Request类两个明显特点:

  1. 构造函数是私有的,只能通过类的内部调用,我们看到是Build方法调用
  2. 有个静态的内部类Builder,构建Request实例只能通过Builder

使用

Request request = new Request.Builder()
                .get()
                .url("https:www.baidu.com")
                .build();

网站栏目:okhttp中的Builder模式-创新互联
网页链接:http://bzwzjz.com/article/gsijc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 攀枝花网站设计 网站建设 成都网站建设公司 成都网站设计 网站制作公司 成都网站设计公司 品牌网站建设 成都营销网站制作 成都企业网站制作 成都网站建设 手机网站制作 成都网站设计 成都网站建设 成都响应式网站建设公司 定制网站设计 LED网站设计方案 营销型网站建设 成都网站设计 成都网站建设 H5网站制作 重庆网站建设 四川成都网站建设