groostav
01/04/2017, 11:02 AMinterface ComponentController {
var thingy: Thingy
fun thingyProperty: Property<Thingy> //this follows a javafx & tornado FX beans convention
//some other props, _mostly_ exposing a read model
companion object {
internal fun ComponentController.doCommonSetupLogic(value: Value) {
this.thingy = doComputation(value)
//...
}
}
}
class ComponentTypeA: ComponentController {
init {
val value = derriveValueFromProcess1()
doCommonSetupLogic(value)
}
}
class ComponentTypeB: ComponentController {
init {
val value = derriveValueFromProcess2()
doCommonSetupLogic(value)
}
}
the advantages here over standard inheritance are few but important in my eyes:
- static dispatch means futher subclassing is discouraged, which is perfectly acceptable with our use of MVC in javafx --nobody subclasses controllers.
- the interface is very concise, and exposes the entirety of the read and write models available to the extension methods. In this sense, if you have long methods shared between two classes, they're not over-powering your interface as a base class.
- we keep to interfaces and concrete implementations, avoiding any abstract classes.
It might all turn out in disaster, but its fun to play with 🙂