Andrew Ebling
05/05/2021, 11:35 AMfun myFunction()
. The body of both implementations is different, but the last line is the same, calling a function in the abstract superclass.
The common last line must be executed last. What is the most appropriate way to use Kotlin language features to refactor out the duplication?José González Gómez
05/05/2021, 11:37 AMfun
in the abstract class which is called by myFunction
and override that in the child classes?Andrew Ebling
05/05/2021, 11:40 AMAndrew Ebling
05/05/2021, 11:41 AMFleshgrinder
05/05/2021, 11:41 AMabstract class AC {
fun myFunction() {
doMyFunction()
// post logic
}
protected abstract fun doMyFunction()
}
José González Gómez
05/05/2021, 11:41 AMabstract class Parent {
abstract fun myFunction()
fun anotherFunction()
}
class FirstChild: Parent {
fun myFunction() {
...
anotherFunction()
}
}
class SecondChild: Parent {
fun myFunction() {
...
anotherFunction()
}
}
Andrew Ebling
05/05/2021, 11:41 AMAndrew Ebling
05/05/2021, 11:44 AMJosé González Gómez
05/05/2021, 11:44 AMabstract class Parent {
fun myFunction() {
this.configurableBehavior()
this.sharedBehavior()
}
fun sharedBehavior() {
...
}
abstract fun configurableBehavior()
}
class FirstChild: Parent {
fun configurableBehavior() {
...
}
}
class SecondChild: Parent {
fun configurableBehavior() {
...
}
}
Andrew Ebling
05/05/2021, 11:45 AMJosé González Gómez
05/05/2021, 11:45 AMAndrew Ebling
05/05/2021, 11:46 AMFleshgrinder
05/05/2021, 12:03 PMExactly
in my comment I meant that @José González Gómez’s comment is exactly correct (I think it was confusing with the sentence that followed 😄 ).