Consider the problem:
We have a Base
class with an abstract method. Now, we would like to enforce that every override of this method will perform some argument checking or some other drudgery. We want this argument checking to be identical across all overrides. One solution would be to wrap this behavior in a non-abstract method which calls an abstract one:
abstract class Base
{
fun computeSomething(argument: Int): Int
{
require(argument > 0) // Some intricate checking
return execute(argument)
}
// Pure functionality, assuming correct arguments
// Ideally, this would be private.
protected abstract fun execute(validArgument: Int): Int
}
class Derived1: Base()
{
override fun execute(validArgument: Int): Int =
validArgument * validArgument
}
class Derived2: Base()
{
override fun execute(validArgument: Int): Int =
validArgument * validArgument * validArgument
}
fun main(args: Array<String>)
{
val d = Derived1()
println(d.computeSomething(23))
}
I would like to have Base::execute
private, such that no subclass of Base
can call it by accident without checking its arguments. Unfortunately, this is not possible:
Error:(9, 5) Kotlin: Modifier 'private' is incompatible with 'abstract'
Kotlin's protected
is better than Java's protected
, since it makes the function accessible only to subclasses but still, the ideal case would be private
.
So is there some right way to implement this pattern? Also, just out of curiosity, is the incompatibility of private
and abstract
a deliberate choice of language design?
Aucun commentaire:
Enregistrer un commentaire