Understanding MVVM Architecture
Introduction
Working with an architectural pattern is essential to keeping your project loosely coupled, which means keeping all the components of our project separate so that each component has little or no knowledge about the others. This is especially important in large projects because things become unmanageable very quickly. That’s why we have architecture patterns to make our project modular, where each component has a specific responsibility and modifications are possible without modifying other modules.
In Android, we can use any one of the architectural patterns, as each has its advantages and disadvantages (which we are not going to discuss here). But it is highly recommended by Google and the Android development team to use the MVVM architecture.
Benefits of Architecture
Having a good architecture implemented in your app brings a lot of benefits to the project and engineering teams:
It improves the maintainability, quality and robustness of the overall app.
It allows the app to scale. More people and more teams can contribute to the same codebase with minimal code conflicts.
It helps with onboarding. As Architecture brings consistency to your project, new members of the team can quickly get up to speed and be more efficient in less amount of time.
It is easier to test. A good Architecture encourages simpler types which are generally easier to test.
Bugs can be investigated methodically with well-defined processes.
Investing in architecture also has a direct impact on your users. They benefit from a more stable application and more features due to a more productive engineering team.
What is MVVM?
MVVM means "Model-View-ViewModel".
MVVM pattern helps to cleanly separate the business and presentation logic of an application from its user interface (UI)
MVVM architecture facilitates a separation of development of the graphical user interface with the help of mark-up language or GUI code.
The view model of MVVM is a value converter which means that it is the view model’s responsibility for exposing the data objects from the Model in such a way that objects are easily managed and presented.
There are mainly 3 components of MVVM architecture, namely:
Activity/Fragment: These are the classes in which we place the different views with which the user interacts. So, this is a UI part or we can say View of this architecture.
ViewModel: A
ViewModel
class is a class where we perform functions either which are related to the business logic or independent of the UI.But why do we need a
ViewModel
, I mean that there can another class in which we put our business logic and then use that class in our view (Activity/Fragment).In the above case, the class will be bound to the View’s lifecycle, and if a case occurs where the view is destroyed by the user’s action or some device event that is out of the developer’s hand. In that case, the data is will be destroyed with that. For example, your app may include a list of users in one of its activities. When the activity is re-created for a configuration change, the new activity has to re-fetch the list of users. To avoid this kind of scenarios, we make use of
ViewModel
by storing such data in its variables.Repository: In this class, we perform the tasks which are related to the data sources which can be an
API
or a localDataBase
. Since we get the data that flows in the app from the repository, we can say that this our Model.