Reputation: 2418
I have a few classes that from my client code I need to call. Right now I have a list and then the client has to check the list for the implementation. It is a mess to use.
object Adapter {
val list = listOf(
A(),
B()
...
)
}
Ideally I would only keep one implementation in memory, but sometimes I need to change between implementations.
Upvotes: 2
Views: 486
Reputation: 7824
Make all your classes implement an interface. Since they all implement the same interface you can then assign the concrete instances to the same variable.
interface IWorker {
fun doWork()
}
Then in the Adapter class you can set which implementation you want to use.
object Adapter {
var worker: IWorker = Default()
}
Default represents any of your classes.
class Default: IWorker {
override fun doWork() {}
}
Upvotes: 2