Navjyot Singh
Navjyot Singh's Blog

Navjyot Singh's Blog

What is MVVM and Why do most developer prefer it?

What is MVVM and Why do most developer prefer it?

Navjyot Singh's photo
Navjyot Singh
·Jun 7, 2021·

5 min read

Learning the basics of Android application development and then directly starting implementing it in some real-world project is not at all a difficult task but Making an Android Application that has a firm structure, capabilities of scaling up, and is maintainable at the same time is a whole different story.

The concept of OOPs comes in here i.e code should be easily maintained and instead of making a single class with all the functions, we should divide it into smaller parts.

Note: Before starting I am assuming that you have a little programming knowledge or you must have heard of Android development.

So the next question arises, How can we achieve all of this? The solution is by using the architectural patterns and currently, there are three most popular design patterns used in android development i.e MVC, MVP, and MVVM. Out of these MVVM is actively supported by Google and that’s one of the main reasons contributing to its popularity. There are other reasons as well and we will talk about them by the end of this article

What is MVVM?

mvvm.png

MVVM stands for Model view viewModel. Separation of logic/code is a beautiful thing and every single design pattern aims at achieving it in the best possible way possible. In the case of MVVM, the separation of concern is done using the Model, View, and View model. Also, there is one more part in this i.e repository, you can simply call it a single source of communication for the different data parts.

View This view should not be considered as a base class for textView or Recycler view instead it is that part of the application which is responsible for the interaction with the user. In simple words, it can be defined as that part of the application that the user sees and handles all the user clicks or gestures.

The most important part here is that “View only handles the immediate user interactions only”. That simply means that there should not be any business logic or any complex functionalities residing inside the Activities or fragments handling the view. The main catch here is that views can only display the stuff that they get from viewModel.

ViewModel The next part that comes in the MVVM hierarchy is the viewModel. It can be considered as a medium between the Repository and view. The viewModel gets the data from Repository and makes it available for usage by View.

When you will have a look at the flow chart above you’ll be wondering now that how does the view gets the data to show. The arrow is pointing in just one direction i.e towards viewModel. The solution to this is by making the data in the viewModel observable. By doing this the view can keep an eye on the data and change accordingly when the data changes. When there is some change in data all the views which are observed get notified.

LiveData is the predefined data type that is used to achieve this observe and implement lifecycle. It is packed with the capability of automatically notifying all the observers of the data.

One more interesting thing you can now easily conclude is that ViewModel doesn’t have any hint about View. This makes it perfect for testing as there are fewer interactions/communications between classes.

Model This is the part where all the business logic of the application comes. But technically according to the hierarchy there is one additional part i.e repository. From where you can regard every type of database or data interaction (backend of application). Repository handles the app’s data, fetching it from the network and applying the business logic.

A repository is a single data source for viewModel. If viewModel needs some data, it gets directly from the repository. That’s up to the repository that from where the data is going to be coming from or what is the data

Some More information Like the ViewModel is not aware of how the view is using or handling the data similarly repository is not aware of how that data is going to get utilized in viewModel.

To make it easier some additional dotted lines have been made on the chart. This means that the upper class has a direct reference to the child, but the child doesn't have any of the parents. Children can expose the data by allowing it to be observed through the liveData or any equivalent library used.

For a better understanding, you should always adhere to the flow that is depicted in the flow chart for a neat and clean implementation of MVVM.

Why MVVM is so popular? After understanding it you must be wondering why MVVM is so popular and not others?. The answer to this is the presence of the View Model. It is an implementation of observer design pattern, and therefore changes made in the model are observed directly in view as well. For example, we change the slider value the data in the model is not only changed but the data which may be a text, that is dismayed along with the view is updated as well. MVVM helps in achieving two-way binding making it lesser complex while maintaining the states and is more automated as compared to the other two.

A simple one-liner difference MVC - The old traditional way

MVP - More popular way because of its low-coupling. The presenter acts as a mediator between the view and Model

MVVM- There's a two way binding between VM and UI components, so it is comparatively more automated than the other two

Conclusion Mvvm is an acronym for Model View ViewModel, one of the most popular architectures in the Android development world. It aims at reducing the complexity of code, making it more modular and easy to scale. Also, due to its observer design pattern, it has gained a lot of traction and it is indeed more automated as compared to the other two, which can also be the reason behind the largest tech giant supporting it actively. However, if you learned anything from this article or you think you require this at any point in time consider bookmarking it.

 
Share this