Inspired by this post, I tried to develop an app using the MVVM architecture pattern.
This architecture pattern presents three different parts:
The view is the responsible of interact with the UI. In iOS is de UIView and UIViewController classes. The Model represent the data to be managed. An the ViewModel becomes an interactor between Model and View. But the special thing is how thos interactor is managed by the View.
The ViewModel must declare a variable for each data that must be showed in the View. Then the View must declare a observer of the variable. This observer is invoked everytime the variable change, and then the View must present the new value to the user. Like the following code example:
Declaring a ViewModel this way we must design a concrete class for each View. This class must declare every data that the View requires. The ViewModel can preprocess every data before present it: like translate text, compound mixed strings(name, surname), change meassurament units, etc.
ViewModel can be also responsible for controlling navigation of the Views.
The benefits of this design are:
Separate responsabilities. View can be the only responsible of managing the UI.
Testability. Separating the Model from the View using a third class is the best way to test any of both without the other.
I tried this achitecture in an app, and I really liked it.
Instead of creating one didChangeXXXXXX method for every property in the ViewModel, create one method didChange as an map of mehtods and use an enum as the index of the map.