Topics In Demand
Notification
New

No notification found.

Blog
MVVM Architecture For Android

May 31, 2019

765

0

In software engineering, a design pattern is a solved common occurring problem in software design. A design pattern is not a final design that can be transformed directly into 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 change or bugs in some other part of the 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 in a proper manner so that everything works well, in short, a happy developer life. In this article, we are discussing MVVM design pattern in Android and how we can solve existing problems with MVVM design pattern.

The main component in MVVM design pattern are :

  1. View – Informs the ViewModel about the user’s actions
  2. ViewModel – Exposes streams of data relevant to the View
  3. 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 just expose states/data and actually has no idea by whom or how that data will be consumed. Only View(Activity) holds the reference to ViewModel and not vice versa, this solves our tight coupling issue. A single view can hold a reference to multiple ViewModels.

View
In android, activity represents a view in MVVM design pattern.The View is responsible for visual display of applications, along with 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 has been introduced to Android with the birth of DataBinding library.You can  enable databinding from app.gradle.

Add these lines in app.gradle inside android{} tag.  Then put your xml layout code under <layout></layout> tag. This will generate binding class and you can access this class from activity. You can see, we can directly access getText() method from xml.


After setContentView in activity you set a reference of 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 below code snippet, we test 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.

A simple example of the MVVM implementation can be found here:

https://github.com/kuldeepjaiswal/MVVMAndroid

About the Author

Kuldeep has been working on mobile applications (Android and iOS) with Xoxoday for over 4 years. Over his time here, he has become the sought-after lead developer with varied experience, effectively making him jack-of-all-trades developer.


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


Xoxoday

© Copyright nasscom. All Rights Reserved.