Kotlin与泛型和Dagger的接口

时间:2019-05-30 12:46:16

标签: generics kotlin dagger-2

我定义了一个接口,其中具有以下通用类型

interface ActivityService<T> {
  fun getActivities(context: Context, listener: GetActivitiesListener<T>)
  fun addActivity(activity: T, context: Context, listener: AddActivityListener)
}

并通过

实现
class AwsActivityService : ActivityService<Activity> {

override fun addActivity(activity: Activity, context: Context, listener: AddActivityListener) {
    AddActivityAsyncTask(activity, context, object : AddActivityAsyncTask.ActivityTaskListener {
        override fun onTaskComplete(result: Boolean) {
            listener.onTaskComplete(result)
        }
    }).execute()
}

override fun getActivities(context: Context, listener: GetActivitiesListener<Activity>) {
    GetActivitiesAsyncTask(context, object : GetActivitiesAsyncTask.ActivityTaskListener {
        override fun onTaskComplete(activityList: List<Activity>) {
            return listener.onTaskComplete(activityList)
        }
    }).execute()
}

}

然后在实例化AwsActivityService时如何在Dagger中定义模块的泛型?

@Module
class ActivitiesModule() {

    @Provides
    @Inject
    @Singleton
    internal fun provideActivitiesModule(): ActivityService {
        return AwsActivityService()
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用factory pattern根据类型提供多个类。

@Module
class ActivitiesModuleFactory() {

    @Provides
    @Inject
    @Singleton
    internal fun provideActivitiesModuleFactory(): ActivityServiceFactory {
        return ActivityServiceFactory()
    }
}

class ActivityServiceFactory {

  inline fun <reified T: Any> getActivities(context: Context, listener: GetActivitiesListener<T>) =
    when(T::class) {
      GoogleActivity::class -> google.getActivities(context, listener as GetActivitiesListener<GoogleActivity>)
      AwsActivity::class -> aws.getActivities(context, listener as GetActivitiesListener<AwsActivity>)
      else -> //throw error or use default listener here
    }

  fun addActivity(activity: any, context: Context, listener: AddActivityListener)=
    when(activity) {
      is GoogleActivity -> google.addActivity(activity, context, listener)
      is AwsActivity -> aws.addActivity(activity, context, listener)
      else -> //throw error or use default listener here
    }

  companion object { 
    val aws = AwsActivityService()
    val google = GoogleActivityService()
  }
}
GoogleActivityService : ActivityService<GoogleActivity> {
    override fun addActivity(activity: GoogleActivity, context: Context, listener: AddActivityListener) {
      AddActivityAsyncTask(activity, context, object : AddActivityAsyncTask.ActivityTaskListener {
        override fun onTaskComplete(result: Boolean) {
          listener.onTaskComplete(result)
        }
    }).execute()
    }

    override fun getActivities(context: Context, listener: GetActivitiesListener<GoogleActivity>) {
      GetActivitiesAsyncTask(context, object : GetActivitiesAsyncTask.ActivityTaskListener {
        override fun onTaskComplete(activityList: List<GoogleActivity>) {
          return listener.onTaskComplete(activityList)
        }
    }).execute()
 }

创建带有伴侣对象的工厂的基本思想,该伴侣对象包含所需的每种类型的实例。该工厂的接口可以接受任何类型,然后使用带有类型检查的when语句来调用正确的活动服务。由于伴随对象已绑定到该类,因此每个ActivityService都只有一个。