Android MVVM

Why we need any Design Pattern?

If you are writing everything in single Activity or Fragment, in this case, it would create an issue in testing and refactoring code.
So for a clean code architecture Design Pattern is recommended.

You need an architecture pattern that allows the fast reaction to design changes,
The solution is MVVM.

MVVM -> Model View ViewModelM : Model (Abstract the data source. the ViewModel works with the Model to get and save data.)
V : View (That inform the ViewModel about the user's action)
VM : ViewModel (Expose streams of data relevant to the View)
In this tutorial, we'll be discussing and implementing the Android MVVM Architectural Pattern in our Android Application.

Model: This holds the data of the application. It cannot directly talk to the View. Generally, it's recommended to expose the data to ViewModel through Observables.
View: It represents the UI of the application devoid of any application login. It Observes the ViewModel.
ViewModel: It acts as a link between the Model and the View. it's responsible for transforming the data from the Model. It provides data streams to the View. It also uses hooks or callbacks to update the View. it'll ask for the data from the Model.

How MVVM differ from MVP:

There is no Presenter in MVVM, ViewModel replaces the Presenter in the Middle Layer.
The Presenter holds references to the View. The ViewModel doesn't.
The Presenter trigger methods to update the View.
ViewModel sends data stream.
The Presenter and View are in a 1 to 1 relationship.
The View and the ViewModel are in 1 to many relationships.

How To Implement MVVM in Android:

We can implement MVVM in two ways, 
Data Binding

1 comment: