Retrofit设计模式

127 阅读1分钟

建造者模式

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

工厂模式

public interface CallAdapter<T> {
    Type responseType();

    <R> T adapt(Call<R> var1);

    public abstract static class Factory {
        public Factory() {
        }
       (1)返回不同的网络适配器
        public abstract CallAdapter<?> get(Type var1, Annotation[] var2, Retrofit var3);

        protected static Type getParameterUpperBound(int index, ParameterizedType type) {
            return Utils.getParameterUpperBound(index, type);
        }

        protected static Class<?> getRawType(Type type) {
            return Utils.getRawType(type);
        }
    }
}

静态工厂

根据Class.forName返回不同的平台。

private static Platform findPlatform() {
    try {
        Class.forName("android.os.Build");
        if (VERSION.SDK_INT != 0) {
            return new Platform.Android();
        }
    } catch (ClassNotFoundException var3) {
    }

    try {
        Class.forName("java.util.Optional");
        return new Platform.Java8();
    } catch (ClassNotFoundException var2) {
        try {
            Class.forName("org.robovm.apple.foundation.NSObject");
            return new Platform.IOS();
        } catch (ClassNotFoundException var1) {
            return new Platform();
        }
    }
}

外观模式

我们交互只需要和Retrofit打交道,不需要了解它里面封装的子系统。

public final class Retrofit {
    private final Map<Method, ServiceMethod> serviceMethodCache = new LinkedHashMap();
    private final Factory callFactory;
    private final HttpUrl baseUrl;
    private final List<retrofit2.Converter.Factory> converterFactories;
    private final List<retrofit2.CallAdapter.Factory> adapterFactories;
    private final Executor callbackExecutor;
    private final boolean validateEagerly;
}

策略模式

public interface CallAdapter<T> {
    Type responseType();

    (1)策略模式
    <R> T adapt(Call<R> var1);

    public abstract static class Factory {
        public Factory() {
        }

        public abstract CallAdapter<?> get(Type var1, Annotation[] var2, Retrofit var3);

        protected static Type getParameterUpperBound(int index, ParameterizedType type) {
            return Utils.getParameterUpperBound(index, type);
        }

        protected static Class<?> getRawType(Type type) {
            return Utils.getRawType(type);
        }
    }
}

适配器模式

public CallAdapter<Call<?>> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
        return null;
    } else {
        final Type responseType = Utils.getCallResponseType(returnType);
        return new CallAdapter<Call<?>>() {
            public Type responseType() {
                return responseType;
            }

           (1)将OkhttpCall转换为不同平台的Call
            public <R> Call<R> adapt(Call<R> call) {
                return new ExecutorCallAdapterFactory.ExecutorCallbackCall(ExecutorCallAdapterFactory.this.callbackExecutor, call);
            }
        };
    }
}

动态代理模式

public <T> T create(final Class<T> service) {
    Utils.validateServiceInterface(service);
    if (this.validateEagerly) {
        this.eagerlyValidateMethods(service);
    }

    return Proxy.newProxyInstance(service.getClassLoader(), new Class[]{service}, new InvocationHandler() {
        private final Platform platform = Platform.get();

        public Object invoke(Object proxy, Method method, Object... args) throws Throwable {
            if (method.getDeclaringClass() == Object.class) {
                return method.invoke(this, args);
            } else if (this.platform.isDefaultMethod(method)) {
                return this.platform.invokeDefaultMethod(method, service, proxy, args);
            } else {
                ServiceMethod serviceMethod = Retrofit.this.loadServiceMethod(method);
                OkHttpCall okHttpCall = new OkHttpCall(serviceMethod, args);
                return serviceMethod.callAdapter.adapt(okHttpCall);
            }
        }
    });
}