第三方开源库 Retrofit - 源码设计模式分析

Retrofit 这个开源库对我成长还是挺大的,自己虽不常用,但他的源码封装思想,却需要用到实际的开发中。这些年有两本书一直都在看 《Android的源码》和《 JakeWharton的源码》。JakeWharton 映象最深的是自己刚做Android时的 ViewPageIndicator, 那个时候这个库才刚开源,如今又是几个年头过去了,想想一个 Android 的十几年的大神叫我怎能不激动,所以他所有的源码我都不曾放过。

设计模式有很多的理论基础,借此机会我们就看看到底都应该怎么用,也可以巩固之前的一些知识,所以 Retrofit 的源码分析,我们主要来分析他的设计模式,他的封装思想。总结里面的源码经验,方可优化自己的网络引擎。

1.Builder 设计模式

  static {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                // 添加日志打印
                .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Log.d("TAG", message);
                    }
                }).setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                // 主路径
                .baseUrl("http://ppw.zmzxd.cn/index.php/api/v1/")
                // 添加转换工厂
                .addConverterFactory(GsonConverterFactory.create())
                // 配置 OkHttpClient
                .client(httpClient).build();
        // 创建 ServiceApi
        mServiceApi = retrofit.create(ServiceApi.class);
    }

2.动态代理设计模式

public <T> T create(final Class<T> service) {
    Utils.validateServiceInterface(service);
    if (validateEagerly) {
      eagerlyValidateMethods(service);
    }
    return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
        new InvocationHandler() {
          private final Platform platform = Platform.get();

          @Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
              throws Throwable {
            // If the method is a method from Object then defer to normal invocation.
            if (method.getDeclaringClass() == Object.class) {
              return method.invoke(this, args);
            }
            if (platform.isDefaultMethod(method)) {
              return platform.invokeDefaultMethod(method, service, proxy, args);
            }
            ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod<Object, Object>) loadServiceMethod(method);
            OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.callAdapter.adapt(okHttpCall);
          }
        });
  }

create()这个方法,个人认为 Retrofit 能做到解耦就是因为动态代理的设计模式用得好,这种模式我们也是经常用到,有很多的体现形式,比如之前所讲的 Android插件化架构 - 拦截Activity的启动流程绕过AndroidManifest检测 主要是用来 Hook 拦截方法,而 Retrofit 这里主要是用来解耦,后面 MVP 部分我们则主要用做AOP切面编程,等等。

3.工厂设计模式

abstract class Factory {
    /**
 * Returns a {@link Converter} for converting an HTTP response body to {@code type}, or null if
 * {@code type} cannot be handled by this factory. This is used to create converters for
 * response types such as {@code SimpleResponse} from a {@code Call<SimpleResponse>}
 * declaration.
 */
    public @Nullable Converter<ResponseBody, ?> responseBodyConverter(Type type,
        Annotation[] annotations, Retrofit retrofit) {
      return null;
    }

    /**
 * Returns a {@link Converter} for converting {@code type} to an HTTP request body, or null if
 * {@code type} cannot be handled by this factory. This is used to create converters for types
 * specified by {@link Body @Body}, {@link Part @Part}, and {@link PartMap @PartMap}
 * values.
 */
    public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type,
        Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
      return null;
    }

    /**
 * Returns a {@link Converter} for converting {@code type} to a {@link String}, or null if
 * {@code type} cannot be handled by this factory. This is used to create converters for types
 * specified by {@link Field @Field}, {@link FieldMap @FieldMap} values,
 * {@link Header @Header}, {@link HeaderMap @HeaderMap}, {@link Path @Path},
 * {@link Query @Query}, and {@link QueryMap @QueryMap} values.
 */
    public @Nullable Converter<?, String> stringConverter(Type type, Annotation[] annotations,
        Retrofit retrofit) {
      return null;
    }
}

工厂设计模式又分为:简单工厂,抽象工厂,工厂方法。addConverterFactory(GsonConverterFactory.create()) 好处就不用说了吧?当然这个地方还影藏着另一种设计模式,如有不了解你可以点击标题进入相应的链接。

4.Adapter 适配器模式

public interface CallAdapter<T> {
    // 返回请求后,转换的参数Type类型
    Type responseType();
    // 接口适配
    <R> T adapt(Call<R> call);
}

突然发现没写过 Adapter 设计模式的文章只录制了视频,有时间我会补一下的。我们都知道 Retrofit 是支持 RxJava 的,也就是说 OkHttp + RxJava + Retrofit 这本该是一套。我们看下是怎么办到的:

final class RxJavaCallAdapter<R> implements CallAdapter<R, Object> {

  RxJavaCallAdapter(Type responseType, @Nullable Scheduler scheduler, boolean isAsync,
      boolean isResult, boolean isBody, boolean isSingle, boolean isCompletable) {
  }

  @Override public Type responseType() {
    return responseType;
  }

  @Override public Object adapt(Call<R> call) {
    OnSubscribe<Response<R>> callFunc = isAsync
        ? new CallEnqueueOnSubscribe<>(call)
        : new CallExecuteOnSubscribe<>(call);
    // 省略代码
    OnSubscribe<?> func;
    if (isResult) {
      func = new ResultOnSubscribe<>(callFunc);
    } else if (isBody) {
      func = new BodyOnSubscribe<>(callFunc);
    } else {
      func = callFunc;
    }
    Observable<?> observable = Observable.create(func);

    if (scheduler != null) {
      observable = observable.subscribeOn(scheduler);
    }
    return observable;
  }
}

这里用自己的话总结就是 RxJavaCallAdapter 实现了 CallAdapter 目标接口,调用 adapt 方法把 Call 转换适配成了 Observable。再通俗一点就是我想要的是 RxJava 的 Observable 对象,但是我只有 Call 这个怎么办?所以采用适配器模式。

5.模板设计模式

abstract class ParameterHandler<T> {
  abstract void apply(RequestBuilder builder, @Nullable T value) throws IOException;

  static final class Query<T> extends ParameterHandler<T> {
    private final String name;
    private final Converter<T, String> valueConverter;
    private final boolean encoded;

    Query(String name, Converter<T, String> valueConverter, boolean encoded) {
      this.name = checkNotNull(name, "name == null");
      this.valueConverter = valueConverter;
      this.encoded = encoded;
    }

    @Override void apply(RequestBuilder builder, @Nullable T value) throws IOException {
      if (value == null) return; // Skip null values.

      String queryValue = valueConverter.convert(value);
      if (queryValue == null) return; // Skip converted but null values

      builder.addQueryParam(name, queryValue, encoded);
    }
  }
}

不到 20 个类,23 种设计模式 Retrofit 运用占了一大半,像 观察者设计模式、策略设计模式,享元设计模式、门面设计模式、单例设计模式、原型设计模式 、装饰设计模式 等等都在其源码中有体现。

抛出一个问题,Retrofit.Builder 中有一个 baseUrl 用作主路径地址,但是开发中可能会出现多个 baseUrl 这种情况,不知看了源码后有没有更好的解决方案。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
retrofit-spring-boot-starter是一个用于整合Retrofit和Spring Boot的starter项目,它可以简化在Spring Boot中使用Retrofit的配置和使用。 以下是retrofit-spring-boot-starter的使用方法: 1. 在你的Spring Boot项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.lianjiatech</groupId> <artifactId>retrofit-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> ``` 2. 创建一个接口,用于定义Retrofit的API接口。例如: ```java import retrofit2.http.GET; import retrofit2.http.Path; public interface MyApi { @GET("/users/{username}") User getUser(@Path("username") String username); } ``` 3. 在你的Spring Boot应用程序中,使用`@Autowired`注解将Retrofit的API接口注入到你的代码中。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import retrofit2.Retrofit; @Service public class MyService { private final MyApi myApi; @Autowired public MyService(Retrofit retrofit) { this.myApi = retrofit.create(MyApi.class); } public User getUser(String username) { return myApi.getUser(username); } } ``` 4. 现在你可以在你的代码中使用`MyService`来调用Retrofit的API接口了。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } @GetMapping("/users/{username}") public User getUser(@PathVariable String username) { return myService.getUser(username); } } ``` 以上是retrofit-spring-boot-starter的基本用法。你可以根据自己的需求进行配置和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥一号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值