What is MVVM architecture?
Developers always prefer a clean and structured code for the projects. By organizing the codes according to a design pattern helps in the maintenance of the software. By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features. Further, design patterns also assure that all the codes get covered in Unit Testing without the interference of other classes. Model — View — ViewModel (MVVM) is the industry
MVVM architecture is a Model-View-ViewModel architecture that removes the tight coupling between each component. Most importantly, in this architecture, the children don't have the direct reference to the parent, they only have the reference by observables.
Model: It represents the data and the business logic of the Android Application. It consists of the business logic - local and remote data source, model classes, repository.
View: It consists of the UI Code(Activity, Fragment), XML. It sends the user action to the ViewModel but does not get the response back directly. To get the response, it has to subscribe to the observables which ViewModel exposes to it.
ViewModel: It is a bridge between the View and Model(business logic). It does not have any clue which View has to use it as it does not have a direct reference to the View. So basically, the ViewModel should not be aware of the view who is interacting with. It interacts with the Model and exposes the observable that can be observed by the View.
This is all about the MVVM, now let's move to the implementation part of it.
Set up a new project with Kotlin and other dependencies required
Here, we are going to set up the Android Project.
Create a Project
Start a new Android Studio Project
Select Empty Activity and Next
Package name: com.mindorks.framework.mvvm
Your starting project is ready now
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.