I currently work on an Interface which which has a simple function that all classes that extends this interface should implement. BUT the classes should be callable with different function arguments like this
interface IDoSomething<T> {
fun execute(vararg any: Any?): T // Make function arguments interchangeable
}
class DoSomethingA : IDoSomething<String> {
// This is what i want
override fun execute(int: Int, boolean: Boolean): String {
println("Do something with parameters: $int, $boolean")
...
}
// This is what i need to do
override fun execute(vararg any: Any?): String {
val int = any[0] as Int
val boolean = any[1] as Boolean
println("Do something with parameters: $int, $boolean")
...
}
}
Other classes that implement this interface should be able to have other arguments
class DoSomethingB : IDoSomething<String> {
// Should also be possible with same interface
override fun execute(string: String, double: Double, boolean: Boolean): String {
println("Do something with parameters: $string, $double, $boolean")
...
}
}
Is there anything in Kotlin which can help me to do thinks like this ? Or exist a pattern which helps to solve exactly this kind of problem.