OOP,在大项目中使用两种不同的Retrofit实现的最佳实践是什么?

时间:2017-01-05 12:19:48

标签: android oop design-patterns retrofit2

我的情况是我需要Retrofit实例进行缓存,但大多数情况下我使用默认的Retrofit实例,我知道缓存需要使用OkHttp客户端,但我需要的是一种优雅的方法使用这两个实例而不复制代码..

我认为我可以使用Dagger来处理这个问题,但这是一个旧项目,我试图一次增强一个部分。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

只需创建两个Retrofit实例。一个有缓存&一个没有缓存。

 public static <S> S  createService(Class<S> yourApiClass) {

        Retrofit retrofit = builder.client(httpClient.build()).build();
        return return retrofit.create(yourApiClass);

    }

 public static <S> S  createServiceWithCache(Class<S> yourApiClass) {

            //add your cache logic here
            return return retrofit.create(yourApiClass);

        }