It looks like I have been using MVP without knowing it. In fact, I thought I was using MVC. I came to know about this MVVM while learning Android. I like MVVM because of the unidirectional data flow, which makes the architecture a lot cleaner.
The diagram below gives you side-by-side comparisons between them.
- Dependencies direction: View -> Model, Controller -> Model and View
- View knows about Model (e.g. data binding), Controller knows everything, Model knows nothing
- Controller observes UI event from View, writes data to the Model and tells the View what to update
- View retrieves the data from model to display
There are different variations of MVC out there. So what I shared here is based on my understanding which I have tweaked a bit from the original MVC design.
- Dependencies direction: Presenter -> Model and View
- Presenter knows everything, View and Model know nothing
- Presenter observes UI event from View, writes data to the Model, read the data from Model and provides data to the View for display
MVP is similar to MVC except the View doesn't depends on the Model. The Presenter takes care of reading/writting the data and passsing the data to the View.
- Dependencies direction: View -> View Model -> Model
- View knows about View Model, View Model knows about Model, Model knows nothing
- When there is a UI event, View informs the View Model about the UI event.
- View Model performs some logics and writes data to the Model
- View Model observes the data changes from Model and update its own data
- View observes the data changes from View Model. If there are any changes, View is responsible to update the UI
Please note that the Activity/Fragment has been moved up to View. Also, the dependency and data flow are unidirectional.
I have personally seen hybrid MVC and MVP usage at my work. It makes the architecture very messy. We should really just stick to either one.
MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing.
All my projects(written in Kotlin for Android app) are based on MVVM.