MVVM Architecture
MVVM Architecture
Short Note
MVVM stands for Model, View, ViewModel.
● Model:
This is the data layer of the application. Data layer classes should not directly
communicate with the View. Communications should happen through the
ViewModels.
● View:
This is the presentation(User Interface) layer of the application . It
observes(LiveData or Flow) the ViewModel. It should not hold any application
logic related codes.
● ViewModel:
ViewModel means "a model for a view". Usually each activity has its own
ViewModel. ViewModel acts as a link between the Model and the View(activity
and fragments). It’s responsible for transforming the data from the Model and
providing them to the View. It also sends data(user inputs) from the view to the
model. In some situations, It also uses callbacks to update the View.
This diagram(copied from google's official documentation) shows the architecture
diagram we follow in Android MVVM projects.
What is the difference between MVVM architecture and MVVM clean architecture?
In MVVM clean architecture, when required we add use case classes between the
ViewModel and Repository. Also we define interfaces first and then create their
implementation classes for many data layer components. MVVM clean architecture is
just a more readable, expandable and testable improvement of MVVM architecture.
MVVM pattern has some similarities with the MVP(Model — View — Presenter) design
pattern as the Presenter role is played by ViewModel. However, the drawbacks of the
MVP pattern has been solved by MVVM in the following ways:
1. ViewModel does not hold any kind of reference to the View.
2. Many to-1 relationships exist between View and ViewModel.
3. No triggering methods to update the View.
Ways to Implement MVVM in the Project
There are 2 ways to implement MVVM design pattern in Android projects:
1. Using the DataBinding library released by Google
2. Using any tool like RxJava for DataBinding.
Data Binding:
Google releases the Data Binding Library for Android that allows the developers to bind
UI components in the XML layouts with the application’s data repositories. This helps in
minimizing the code of core application logic that binds with View. Further, Two – way
Data Binding is done for binding the objects to the XML layouts so that object and the
layout both can send data to each other. This point can be visualized by the example of
this tutorial.
Syntax for the two way data binding is @={variable}
Advantages of MVVM Architecture
● Enhance the reusability of code.
● All modules are independent which improves the testability of each layer.
● Makes project files maintainable and easy to make changes.
Disadvantages of MVVM Architecture
● This design pattern is not ideal for small projects.
● If the data binding logic is too complex, the application debug will be a little
harder.
This architecture
pattern is more
It resolves the problem of
UI(View) and event-driven as it
having a dependent View by
data-access uses data binding
using Presenter as a
mechanism(Model) are and thus makes easy
communication channel
tightly coupled. separation of core
between Model and View.
business logic from
the View.
Ideal for small scale Ideal for simple and complex Not ideal for small
projects only. applications. scale projects.