科特林,减少重复的代码

科特林,减少重复的代码

问题描述:

每次我的API服务接口类必须创建静态方法,科特林,减少重复的代码

interface AuthApiService { 
    @FormUrlEncoded 
    @POST("api/auth/login") 
    fun postLogin(@Field("username") username: String, @Field("password") password: String): 
      io.reactivex.Observable<LoginApiResponse> 

    companion object Factory { 
     fun create(): AuthApiService { 
      val gson = GsonBuilder().setLenient().create() 

      val retrofit = Retrofit.Builder() 
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
        .addConverterFactory(GsonConverterFactory.create(gson)) 
        .baseUrl("http:192.168.24.188:8080") 
        .build() 
      return retrofit.create(AuthApiService::class.java) 
     } 
    } 
} 


interface BBBApiService { 
    companion object Factory { 
     fun create(): BBBApiService { 
      val gson = GsonBuilder().setLenient().create() 

      val retrofit = Retrofit.Builder() 
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
        .addConverterFactory(GsonConverterFactory.create(gson)) 
        .baseUrl("http:192.168.24.188:8080") 
        .build() 
      return retrofit.create(BBBApiService::class.java) 
     } 
    } 
} 

但是,我想只定义一次create()方法。

所以我做了ApiFactory类,

interface ApiFactory { 
    companion object { 
     inline fun <reified T>createRetrofit(): T { 
      val gson = GsonBuilder().setLenient().create() 

      val retrofit = Retrofit.Builder() 
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
        .addConverterFactory(GsonConverterFactory.create(gson)) 
        .baseUrl("http://192.168.24.188:8080") 
        .build() 

      return retrofit.create(T::class.java) 
     } 


    } 
} 

interface AuthApiService { 
    @FormUrlEncoded 
    @POST("api/auth/login") 
    fun postLogin(@Field("username") username: String, @Field("password") password: String): 
      io.reactivex.Observable<LoginApiResponse> 

    companion object Factory { 
     fun create(): AuthApiService { 
      return ApiFactory.createRetrofit() 
     } 
    } 

但是,尽管如此,我需要定义AuthApiService的create()方法。

是否有任何一种方法实现了ApiFactory类到SubApi类,以便我不必在每个子类中定义create方法?

一个简单的办法就是直接打电话给你ApiFactory的功能:

val authApiService = ApiFactory.createRetrofit<AuthApiService>() 

但是,如果您希望能够拨打AuthApiService.create(),则可以定义标记界面,例如ApiFactoryClient<T>,并用它标记空的companion object

interface ApiFactoryClient<T> 

interface AuthApiService { 
    /* ... */ 

    companion object : ApiFactoryClient<AuthApiService> 
} 

然后再做一个扩展功能,与ApiFactoryClient<T>工作:

inline fun <reified T> ApiFactoryClient<T>.create(): T = ApiFactory.createRetrofit<T>() 

而且用法是:

val authApiService = AuthApiService.create() 

您可以通过修改ApiFactory这样的:

interface ApiFactory { 
    companion object { 
     inline fun <reified T>createRetrofit(klass: KClass<T>): T { 
      val gson = GsonBuilder().setLenient().create() 

      val retrofit = Retrofit.Builder() 
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
        .addConverterFactory(GsonConverterFactory.create(gson)) 
        .baseUrl("http://192.168.24.188:8080") 
        .build() 

      return retrofit.create(klass.java) 
     } 


    } 
} 

,然后用它来创建不同的服务实例:

val authApiService = ApiFactory.createRetrofit(AuthApiService::class) 
+2

我觉得没有必要改变签名。为什么不简单地调用函数作为'val authApiService = ApiFactory.createRetrofit ()'? – hotkey

+0

@hotkey你是对的,这应该可以正常工作,我只是Kotlin的新手 – ledniov