建造者模式有名创造者模式,是将一个复杂对象的构建过程与它的表示分离,从而使得相同的构建过程可以创建不同的表示;创造者模式隐藏了对复杂对象的创建过程,它把复杂对象的构建加以抽象,通过子类继承或者重载的方式,动态的创建具有 复合属性的对象。
package com.example.administrator.androidtestdemo.chain;
import android.util.Log;
public class Request {
final String name;
final String reason;
final int days;
final String groupLeaderInfo;
final String managerInfo;
final String departmentHeaderInfo;
final String customInfo;
public Request() {
this(new Builder());
}
public Request(Builder builder) {
this.name = builder.name;
this.reason = builder. reason;
this.days = builder.days;
this.groupLeaderInfo = builder. groupLeaderInfo;
this.managerInfo = builder.managerInfo;
this.departmentHeaderInfo = builder.departmentHeaderInfo;
this.customInfo = builder.customInfo;
}
public String name() {
return name;
}
public String reason() {
return reason;
}
public int days() {
return days;
}
public String groupLeaderInfo() {
return groupLeaderInfo;
}
public String managerInfo() {
return managerInfo;
}
public String departmentHeaderInfo() {
return departmentHeaderInfo;
}
public String customInfo() {
return customInfo;
}
public static final class Builder {
String name;
String reason;
int days;
String groupLeaderInfo;
String managerInfo;
String departmentHeaderInfo;
String customInfo;
public Builder(Request request) {
this.name = request.name;
this.reason = request. reason;
this.days = request.days;
this.groupLeaderInfo = request. groupLeaderInfo;
this.managerInfo = request.managerInfo;
this.departmentHeaderInfo = request.departmentHeaderInfo;
this.customInfo = request.customInfo;
}
public Builder() {
name="VanHua";
reason="华仔";
days=1080;
groupLeaderInfo="VanHua";
managerInfo="VanHua";
departmentHeaderInfo="VanHua";
customInfo="VanHua";
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder reason(String reason) {
this.reason = reason;
return this;
}
public Builder days(int days) {
this.days = days;
return this;
}
public Builder groupLeaderInfo(String groupLeaderInfo) {
this.groupLeaderInfo = groupLeaderInfo;
return this;
}
public Builder managerInfo(String managerInfo) {
this.managerInfo = managerInfo;
return this;
}
public Builder departmentHeaderInfo(String departmentHeaderInfo) {
this.departmentHeaderInfo = departmentHeaderInfo;
return this;
}
public Builder customInfo(String customInfo) {
this.customInfo = customInfo;
return this;
}
public Request build() {
return new Request(this);
}
}
public void show(){
Request request=new Builder()
.name("VanHua")
.reason("VanHua")
.days(1080)
.groupLeaderInfo("VanHua")
.managerInfo("VanHua")
.departmentHeaderInfo("VanHua")
.customInfo("VanHua")
.build();
Log.e("CHAIN",request.toString());
}
@Override
public String toString() {
return "Request{" +
"name='" + name + '\'' +
", reason='" + reason + '\'' +
", days=" + days +
", groupLeaderInfo='" + groupLeaderInfo + '\'' +
", managerInfo='" + managerInfo + '\'' +
", departmentHeaderInfo='" + departmentHeaderInfo + '\'' +
", customInfo='" + customInfo + '\'' +
'}';
}
}
结合Okhttp理解
package okhttp3;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.UnknownHostException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.Call.Factory;
import okhttp3.internal.Internal;
import okhttp3.internal.Util;
import okhttp3.internal.cache.InternalCache;
import okhttp3.internal.connection.RealConnection;
import okhttp3.internal.connection.RouteDatabase;
import okhttp3.internal.connection.StreamAllocation;
import okhttp3.internal.platform.Platform;
import okhttp3.internal.tls.CertificateChainCleaner;
import okhttp3.internal.tls.OkHostnameVerifier;
public class OkHttpClient implements Cloneable, Factory {
private static final List DEFAULT_PROTOCOLS;
private static final List DEFAULT_CONNECTION_SPECS;
final Dispatcher dispatcher;
final Proxy proxy;
final List protocols;
final List connectionSpecs;
final List interceptors;
final List networkInterceptors;
final ProxySelector proxySelector;
final CookieJar cookieJar;
final Cache cache;
final InternalCache internalCache;
final SocketFactory socketFactory;
final SSLSocketFactory sslSocketFactory;
final CertificateChainCleaner certificateChainCleaner;
final HostnameVerifier hostnameVerifier;
final CertificatePinner certificatePinner;
final Authenticator proxyAuthenticator;
final Authenticator authenticator;
final ConnectionPool connectionPool;
final Dns dns;
final boolean followSslRedirects;
final boolean followRedirects;
final boolean retryOnConnectionFailure;
final int connectTimeout;
final int readTimeout;
final int writeTimeout;
public OkHttpClient() {
this(new OkHttpClient.Builder());
}
private OkHttpClient(OkHttpClient.Builder builder) {
this.dispatcher = builder.dispatcher;
this.proxy = builder.proxy;
this.protocols = builder.protocols;
this.connectionSpecs = builder.connectionSpecs;
this.interceptors = Util.immutableList(builder.interceptors);
this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
this.proxySelector = builder.proxySelector;
this.cookieJar = builder.cookieJar;
this.cache = builder.cache;
this.internalCache = builder.internalCache;
this.socketFactory = builder.socketFactory;
boolean isTLS = false;
ConnectionSpec spec;
for(Iterator var3 = this.connectionSpecs.iterator(); var3.hasNext(); isTLS = isTLS || spec.isTls()) {
spec = (ConnectionSpec)var3.next();
}
if (builder.sslSocketFactory == null && isTLS) {
X509TrustManager trustManager = this.systemDefaultTrustManager();
this.sslSocketFactory = this.systemDefaultSslSocketFactory(trustManager);
this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
} else {
this.sslSocketFactory = builder.sslSocketFactory;
this.certificateChainCleaner = builder.certificateChainCleaner;
}
this.hostnameVerifier = builder.hostnameVerifier;
this.certificatePinner = builder.certificatePinner.withCertificateChainCleaner(this.certificateChainCleaner);
this.proxyAuthenticator = builder.proxyAuthenticator;
this.authenticator = builder.authenticator;
this.connectionPool = builder.connectionPool;
this.dns = builder.dns;
this.followSslRedirects = builder.followSslRedirects;
this.followRedirects = builder.followRedirects;
this.retryOnConnectionFailure = builder.retryOnConnectionFailure;
this.connectTimeout = builder.connectTimeout;
this.readTimeout = builder.readTimeout;
this.writeTimeout = builder.writeTimeout;
}
private X509TrustManager systemDefaultTrustManager() {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore)null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length == 1 && trustManagers[0] instanceof X509TrustManager) {
return (X509TrustManager)trustManagers[0];
} else {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
} catch (GeneralSecurityException var3) {
throw new AssertionError();
}
}
private SSLSocketFactory systemDefaultSslSocketFactory(X509TrustManager trustManager) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init((KeyManager[])null, new TrustManager[]{trustManager}, (SecureRandom)null);
return sslContext.getSocketFactory();
} catch (GeneralSecurityException var3) {
throw new AssertionError();
}
}
public int connectTimeoutMillis() {
return this.connectTimeout;
}
public int readTimeoutMillis() {
return this.readTimeout;
}
public int writeTimeoutMillis() {
return this.writeTimeout;
}
public Proxy proxy() {
return this.proxy;
}
public ProxySelector proxySelector() {
return this.proxySelector;
}
public CookieJar cookieJar() {
return this.cookieJar;
}
public Cache cache() {
return this.cache;
}
InternalCache internalCache() {
return this.cache != null ? this.cache.internalCache : this.internalCache;
}
public Dns dns() {
return this.dns;
}
public SocketFactory socketFactory() {
return this.socketFactory;
}
public SSLSocketFactory sslSocketFactory() {
return this.sslSocketFactory;
}
public HostnameVerifier hostnameVerifier() {
return this.hostnameVerifier;
}
public CertificatePinner certificatePinner() {
return this.certificatePinner;
}
public Authenticator authenticator() {
return this.authenticator;
}
public Authenticator proxyAuthenticator() {
return this.proxyAuthenticator;
}
public ConnectionPool connectionPool() {
return this.connectionPool;
}
public boolean followSslRedirects() {
return this.followSslRedirects;
}
public boolean followRedirects() {
return this.followRedirects;
}
public boolean retryOnConnectionFailure() {
return this.retryOnConnectionFailure;
}
public Dispatcher dispatcher() {
return this.dispatcher;
}
public List protocols() {
return this.protocols;
}
public List connectionSpecs() {
return this.connectionSpecs;
}
public List interceptors() {
return this.interceptors;
}
public List networkInterceptors() {
return this.networkInterceptors;
}
public Call newCall(Request request) {
return new RealCall(this, request);
}
public OkHttpClient.Builder newBuilder() {
return new OkHttpClient.Builder(this);
}
static {
DEFAULT_PROTOCOLS = Util.immutableList(new Protocol[]{Protocol.HTTP_2, Protocol.SPDY_3, Protocol.HTTP_1_1});
DEFAULT_CONNECTION_SPECS = Util.immutableList(new ConnectionSpec[]{ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT});
Internal.instance = new Internal() {
public void addLenient(okhttp3.Headers.Builder builder, String line) {
builder.addLenient(line);
}
public void addLenient(okhttp3.Headers.Builder builder, String name, String value) {
builder.addLenient(name, value);
}
public void setCache(OkHttpClient.Builder builder, InternalCache internalCache) {
builder.setInternalCache(internalCache);
}
public boolean connectionBecameIdle(ConnectionPool pool, RealConnection connection) {
return pool.connectionBecameIdle(connection);
}
public RealConnection get(ConnectionPool pool, Address address, StreamAllocation streamAllocation) {
return pool.get(address, streamAllocation);
}
public void put(ConnectionPool pool, RealConnection connection) {
pool.put(connection);
}
public RouteDatabase routeDatabase(ConnectionPool connectionPool) {
return connectionPool.routeDatabase;
}
public StreamAllocation callEngineGetStreamAllocation(Call call) {
return ((RealCall)call).streamAllocation();
}
public void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean isFallback) {
tlsConfiguration.apply(sslSocket, isFallback);
}
public HttpUrl getHttpUrlChecked(String url) throws MalformedURLException, UnknownHostException {
return HttpUrl.getChecked(url);
}
public void setCallWebSocket(Call call) {
((RealCall)call).setForWebSocket();
}
};
}
public static final class Builder {
Dispatcher dispatcher;
Proxy proxy;
List protocols;
List connectionSpecs;
final List interceptors = new ArrayList();
final List networkInterceptors = new ArrayList();
ProxySelector proxySelector;
CookieJar cookieJar;
Cache cache;
InternalCache internalCache;
SocketFactory socketFactory;
SSLSocketFactory sslSocketFactory;
CertificateChainCleaner certificateChainCleaner;
HostnameVerifier hostnameVerifier;
CertificatePinner certificatePinner;
Authenticator proxyAuthenticator;
Authenticator authenticator;
ConnectionPool connectionPool;
Dns dns;
boolean followSslRedirects;
boolean followRedirects;
boolean retryOnConnectionFailure;
int connectTimeout;
int readTimeout;
int writeTimeout;
public Builder() {
this.dispatcher = new Dispatcher();
this.protocols = OkHttpClient.DEFAULT_PROTOCOLS;
this.connectionSpecs = OkHttpClient.DEFAULT_CONNECTION_SPECS;
this.proxySelector = ProxySelector.getDefault();
this.cookieJar = CookieJar.NO_COOKIES;
this.socketFactory = SocketFactory.getDefault();
this.hostnameVerifier = OkHostnameVerifier.INSTANCE;
this.certificatePinner = CertificatePinner.DEFAULT;
this.proxyAuthenticator = Authenticator.NONE;
this.authenticator = Authenticator.NONE;
this.connectionPool = new ConnectionPool();
this.dns = Dns.SYSTEM;
this.followSslRedirects = true;
this.followRedirects = true;
this.retryOnConnectionFailure = true;
this.connectTimeout = 10000;
this.readTimeout = 10000;
this.writeTimeout = 10000;
}
Builder(OkHttpClient okHttpClient) {
this.dispatcher = okHttpClient.dispatcher;
this.proxy = okHttpClient.proxy;
this.protocols = okHttpClient.protocols;
this.connectionSpecs = okHttpClient.connectionSpecs;
this.interceptors.addAll(okHttpClient.interceptors);
this.networkInterceptors.addAll(okHttpClient.networkInterceptors);
this.proxySelector = okHttpClient.proxySelector;
this.cookieJar = okHttpClient.cookieJar;
this.internalCache = okHttpClient.internalCache;
this.cache = okHttpClient.cache;
this.socketFactory = okHttpClient.socketFactory;
this.sslSocketFactory = okHttpClient.sslSocketFactory;
this.certificateChainCleaner = okHttpClient.certificateChainCleaner;
this.hostnameVerifier = okHttpClient.hostnameVerifier;
this.certificatePinner = okHttpClient.certificatePinner;
this.proxyAuthenticator = okHttpClient.proxyAuthenticator;
this.authenticator = okHttpClient.authenticator;
this.connectionPool = okHttpClient.connectionPool;
this.dns = okHttpClient.dns;
this.followSslRedirects = okHttpClient.followSslRedirects;
this.followRedirects = okHttpClient.followRedirects;
this.retryOnConnectionFailure = okHttpClient.retryOnConnectionFailure;
this.connectTimeout = okHttpClient.connectTimeout;
this.readTimeout = okHttpClient.readTimeout;
this.writeTimeout = okHttpClient.writeTimeout;
}
public OkHttpClient.Builder connectTimeout(long timeout, TimeUnit unit) {
if (timeout < 0L) {
throw new IllegalArgumentException("timeout < 0");
} else if (unit == null) {
throw new NullPointerException("unit == null");
} else {
long millis = unit.toMillis(timeout);
if (millis > 2147483647L) {
throw new IllegalArgumentException("Timeout too large.");
} else if (millis == 0L && timeout > 0L) {
throw new IllegalArgumentException("Timeout too small.");
} else {
this.connectTimeout = (int)millis;
return this;
}
}
}
public OkHttpClient.Builder readTimeout(long timeout, TimeUnit unit) {
if (timeout < 0L) {
throw new IllegalArgumentException("timeout < 0");
} else if (unit == null) {
throw new NullPointerException("unit == null");
} else {
long millis = unit.toMillis(timeout);
if (millis > 2147483647L) {
throw new IllegalArgumentException("Timeout too large.");
} else if (millis == 0L && timeout > 0L) {
throw new IllegalArgumentException("Timeout too small.");
} else {
this.readTimeout = (int)millis;
return this;
}
}
}
public OkHttpClient.Builder writeTimeout(long timeout, TimeUnit unit) {
if (timeout < 0L) {
throw new IllegalArgumentException("timeout < 0");
} else if (unit == null) {
throw new NullPointerException("unit == null");
} else {
long millis = unit.toMillis(timeout);
if (millis > 2147483647L) {
throw new IllegalArgumentException("Timeout too large.");
} else if (millis == 0L && timeout > 0L) {
throw new IllegalArgumentException("Timeout too small.");
} else {
this.writeTimeout = (int)millis;
return this;
}
}
}
public OkHttpClient.Builder proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public OkHttpClient.Builder proxySelector(ProxySelector proxySelector) {
this.proxySelector = proxySelector;
return this;
}
public OkHttpClient.Builder cookieJar(CookieJar cookieJar) {
if (cookieJar == null) {
throw new NullPointerException("cookieJar == null");
} else {
this.cookieJar = cookieJar;
return this;
}
}
void setInternalCache(InternalCache internalCache) {
this.internalCache = internalCache;
this.cache = null;
}
public OkHttpClient.Builder cache(Cache cache) {
this.cache = cache;
this.internalCache = null;
return this;
}
public OkHttpClient.Builder dns(Dns dns) {
if (dns == null) {
throw new NullPointerException("dns == null");
} else {
this.dns = dns;
return this;
}
}
public OkHttpClient.Builder socketFactory(SocketFactory socketFactory) {
if (socketFactory == null) {
throw new NullPointerException("socketFactory == null");
} else {
this.socketFactory = socketFactory;
return this;
}
}
/** @deprecated */
public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory) {
if (sslSocketFactory == null) {
throw new NullPointerException("sslSocketFactory == null");
} else {
X509TrustManager trustManager = Platform.get().trustManager(sslSocketFactory);
if (trustManager == null) {
throw new IllegalStateException("Unable to extract the trust manager on " + Platform.get() + ", sslSocketFactory is " + sslSocketFactory.getClass());
} else {
this.sslSocketFactory = sslSocketFactory;
this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
return this;
}
}
}
public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager) {
if (sslSocketFactory == null) {
throw new NullPointerException("sslSocketFactory == null");
} else if (trustManager == null) {
throw new NullPointerException("trustManager == null");
} else {
this.sslSocketFactory = sslSocketFactory;
this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
return this;
}
}
public OkHttpClient.Builder hostnameVerifier(HostnameVerifier hostnameVerifier) {
if (hostnameVerifier == null) {
throw new NullPointerException("hostnameVerifier == null");
} else {
this.hostnameVerifier = hostnameVerifier;
return this;
}
}
public OkHttpClient.Builder certificatePinner(CertificatePinner certificatePinner) {
if (certificatePinner == null) {
throw new NullPointerException("certificatePinner == null");
} else {
this.certificatePinner = certificatePinner;
return this;
}
}
public OkHttpClient.Builder authenticator(Authenticator authenticator) {
if (authenticator == null) {
throw new NullPointerException("authenticator == null");
} else {
this.authenticator = authenticator;
return this;
}
}
public OkHttpClient.Builder proxyAuthenticator(Authenticator proxyAuthenticator) {
if (proxyAuthenticator == null) {
throw new NullPointerException("proxyAuthenticator == null");
} else {
this.proxyAuthenticator = proxyAuthenticator;
return this;
}
}
public OkHttpClient.Builder connectionPool(ConnectionPool connectionPool) {
if (connectionPool == null) {
throw new NullPointerException("connectionPool == null");
} else {
this.connectionPool = connectionPool;
return this;
}
}
public OkHttpClient.Builder followSslRedirects(boolean followProtocolRedirects) {
this.followSslRedirects = followProtocolRedirects;
return this;
}
public OkHttpClient.Builder followRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public OkHttpClient.Builder retryOnConnectionFailure(boolean retryOnConnectionFailure) {
this.retryOnConnectionFailure = retryOnConnectionFailure;
return this;
}
public OkHttpClient.Builder dispatcher(Dispatcher dispatcher) {
if (dispatcher == null) {
throw new IllegalArgumentException("dispatcher == null");
} else {
this.dispatcher = dispatcher;
return this;
}
}
public OkHttpClient.Builder protocols(List protocols) {
protocols = Util.immutableList(protocols);
if (!protocols.contains(Protocol.HTTP_1_1)) {
throw new IllegalArgumentException("protocols doesn't contain http/1.1: " + protocols);
} else if (protocols.contains(Protocol.HTTP_1_0)) {
throw new IllegalArgumentException("protocols must not contain http/1.0: " + protocols);
} else if (protocols.contains((Object)null)) {
throw new IllegalArgumentException("protocols must not contain null");
} else {
this.protocols = Util.immutableList(protocols);
return this;
}
}
public OkHttpClient.Builder connectionSpecs(List connectionSpecs) {
this.connectionSpecs = Util.immutableList(connectionSpecs);
return this;
}
public List interceptors() {
return this.interceptors;
}
public OkHttpClient.Builder addInterceptor(Interceptor interceptor) {
this.interceptors.add(interceptor);
return this;
}
public List networkInterceptors() {
return this.networkInterceptors;
}
public OkHttpClient.Builder addNetworkInterceptor(Interceptor interceptor) {
this.networkInterceptors.add(interceptor);
return this;
}
public OkHttpClient build() {
return new OkHttpClient(this, null);
}
}
}