Simple example: Rather than having a base view model that constructs an instance of some kind of API class and then exposes it to its subclasses, simply inject an instance into each view model. Illustrated:
// instead of this:
class BaseViewModel : ViewModel() {
protected val api: MyApi = someComplexApiConstructionLogic()
}
class SomeViewModel : BaseViewModel() {
init {
api.fetchData()
}
}
// prefer this:
class SomeViewModel(val api: MyApi) : ViewModel() {
init {
api.fetchData()
}
}
A lot of older Android projects I've worked on use the first approach, and trust me, composition instead of inheritance makes things much simpler to work with.
Yep. Say you need a view model to conform to this interface, so the fragment using the view model can call foo() and bar():
interface FooBar {
fun foo(): String
fun bar(): String
}
Let's say we're pretty sure the implementation of foo() and bar() are going to be the same for most view models. We can do this by inheritance or composition. Here's the inheritance version:
abstract class BaseViewModel : ViewModel(), FooBar {
override fun foo() = "hello from foo()!"
override fun bar() = "hello from bar()!"
}
There are a couple of problems with this.
What if we decide that we need to reuse this implementation of FooBar outside of a view model? Too bad, this implementation is tightly coupled to BaseViewModel.
What if we need some view models to implement FooBar, some view models to implement some other interface, and some view models to implement both? There's no clean way to do this - we have to introduce a new base class for each permutation of possibilities.
If a subclass of BaseViewModel needs to change the behavior of the FooBar implementation, overriding attributes on the base view model could potentially break the implementation if not designed carefully (this is why Android has the @CallSuper annotation)
There's no way to hide BaseViewModel from the users of its subclasses. That's a problem because here, we're just using BaseViewModel as an implementation detail to help us implement an interfaces. If we ever change how this works, we have to make sure no one was depending on BaseViewModel aside from its subclasses.
Now let's look at the composition approach, using delegation:
class FooBarImpl : FooBar {
override fun foo() = "hello from foo()!"
override fun bar() = "hello from bar()!"
}
class SomeViewModel(fooBarImpl: FooBar) // pass a FooBarImpl here
: ViewModel(), FooBar by fooBarImpl {
// vm stuff here
}
This solves all of the problems mentioned above:
We can reuse FooBarImpl whenever we want, wherever we want!
View models can implement just the interfaces they need to, no matter what combination of interfaces are required.
If any particular view model needs a different implementation of FooBar, we can just swap it out for that view model only instead of (potentially partially) overriding the base class' implementation and breaking it.
The user of the view model doesn't need to know about our FooBar implementation(s) - we can make them hidden from it and not have to worry about accidental reliance on them.
These examples are contrived, but this has been extremely helpful in making code more modular and testable. For this reason, my team recently banned using inheritance unless absolutely necessary as part of using an SDK (e.g. inheriting from Activity).
11
u/MotorolaDroidMofo Sep 29 '20
Simple example: Rather than having a base view model that constructs an instance of some kind of API class and then exposes it to its subclasses, simply inject an instance into each view model. Illustrated:
A lot of older Android projects I've worked on use the first approach, and trust me, composition instead of inheritance makes things much simpler to work with.