MVVM Architecture For Android
In software engineering, a design pattern is a commonly occurring problem in software design. A design pattern is not a final design that can be transformed directly into solved code.
One major problem in software development is the tight coupling of code, i.e. even a small change in one portion of code results in modification or bugs in some other part of code. Code is not unit test friendly, and code is not reusable.
Introduction
Currently, many architectural approaches are available like MVP, FLUX, MVI, MVVM that are solving the above problems. We can use any approach to maintain our code correctly so that everything works well, in short, a happy developer life. This article discusses the MVVM design pattern in android and how we can solve existing problems with the MVVM design pattern.
The main component in the MVVM design pattern are :
- View - Informs the ViewModel about the user’s actions
- ViewModel - Exposes streams of data relevant to the View
- DataModel - Abstracts the data source. The ViewModel works with the DataModel to get and save the data.
Brief Explanation of MVVM Design Pattern
Google introduced Android Architecture Components, which included ViewModel rather than Presenter and hence the proof that even Google is supporting MVVM.
ViewModel
ViewModels are simple classes that interact with the logic/model layer and expose states/data and have no idea by whom or how that data will be consumed. Only View(Activity) references ViewModel and not vice versa; this solves our tight coupling issue. A single view can have a reference to multiple ViewModels.
In Android, activity represents a view in MVVM design pattern. The View is responsible for the visual display of applications and data input by users. This part should not process the data under any circumstances. Its function is only to detect the input like touch or swipe and visualization.
The ModelView-ViewModel architectural pattern was introduced to Android with the birth of the DataBinding library. You can enable data-binding from the app.gradle.
Add these lines in-app.Gradle inside android{} tag. Then put your XML layout code under tag. This will generate a binding class, and you can access this class from the activity. You can see we can directly access the getText() method from XML.
After setContentView inactivity you set a reference of the view model.
DataModel
DataModel holds the data of the application. It cannot directly talk to the View. Generally, it’s recommended to expose the data to the ViewModel through Observables.
Advantages of using MVVM Design Pattern
- Your code testability is increased.
In the below code snippet, we test the getText() method in ViewModel. You can mock your data and check the expected result.
- Your code is decoupled.
- The package structure is even easier to navigate.
- The project is even easier to maintain.
- Your team can add new features even more quickly.
Conclusion
MVVM combines the advantages of separation of concerns provided by MVP while leveraging the advantages of data bindings. The result is a pattern where the model drives as many operations as possible, minimizing the logic in the view.