g.revolution
g.revolution

Reputation: 12262

does MVVM design pattern enforces the ViewModel and View to have a Observable and Observer relationship?

I am learning about MVVM design pattern and trying to implement it in Android application at the moment. I am wondering how changes in a ViewModel are reflected in the View. different guides on internet show different things and some of them are suggesting that ViewModel(Observable) should notify changes to View(Observer) rather then Activity/Fragment update the ViewModel and trigger view to update.

What is the correct/best way to implement this? if I my ViewModel is not a Observable then is it that I am not following MVVM properly?

Upvotes: 0

Views: 561

Answers (1)

Mike Eason
Mike Eason

Reputation: 9713

Generally, the View must be aware of the ViewModel, and the ViewModel must be aware of the Model, but expose the Model to the View. It looks something like this:

View ---knows about---> ViewModel ---knows about---> Model.

It is vital that these arrows stay in this direction, if the Model knows about the ViewModel then all the nice decoupling is lost.

It is the responsibility of the ViewModel to shout out when a property has been changed, and anyone listening (your view) should act accordingly.

I'd recommend taking some time going through this tutorial.

Upvotes: 2

Related Questions