MVVM and DataBinding: Android Design Patterns

This article describes the MVVM Design Pattern and its components, data binding, and other design patterns and architectural concepts for the Android platform. By Matei Suica.

4.5 (47) · 1 Review

Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Searching For More

MVVM is not the end of the story for architectural design patterns. There are a number of other patterns that come in handy on Android, especially as the size of your app grows.

Clean Architecture

In 2012, the concept of a Clean Architecture was developed. Since then, every framework community has tried to include a variation of it.

This concept has four layers:

Where to Go From Here?

VIPER

Clean Architecture separates the code even better than MVVM. That doesn’t mean MVVM is not good, but it is not necessarily as “clean” as it could be. For example, the Model in MVVM can sometimes contain adapters and frameworks. But in Clean Architecture, these are separated out.

clean architecture by Uncle Bob diagram

The most important thing to remember in Clean Architecture is The Dependency Rule. This rule says that source code dependencies can only point inwards in the diagram above, towards the center. Nothing in an inner circle can know anything at all about something in an outer circle.

VIPER is a design pattern that the iOS community developed for their projects. It’s their way of implementing Clean Architecture on iOS.

VIPER stands for: View, Interactor, Presenter, Entity, Router.

As you can see, there are some new components that appear in this acronym. It also suggests that VIPER is a descendant of MVP, not MVVM.

Indeed, you can think of VIPER as MVP with an Interactor and a Router.

The Interactor handles part of the Presenter’s responsibilities in MVP. The business logic moves into the Interactor, leaving the UI events and display to the Presenter.

However, this leaves one problem with the View. The View still handles all the routing between modules. That’s where the Router comes in.

VIPER design pattern flow diagram

In Android, having a Router to handle navigation was tricky. The developer can’t create Activity instances, because the framework does that. There are workarounds so that a Router can be created with Activity instances too. But Google recently announced Fragment improvements in the Support Library and recommends using them, and Fragments can be instantiated by the developer.

Combined with a Single Activity app structure, the Router can take its rightful place in the VIPER design pattern for Android. With Fragments, Routers can have full power over navigating through modules.

That’s a lot of information that you’ve covered in this article!

You now have a good understanding of what design pattens are in software development, with a focus on architectural design patterns.

You know about the most important patterns out there, and the weaknesses of each one. And the most important thing of all: You now know how to implement MVVM, how to test it, and how to use Android’s Architecture Components from Android Jetpack to get the most out of MVVM.

Good job keeping up with all these patterns and components!

To dive deeper into MVVM, you can check out the MVVM on Android video course. You’ll learn more about each of the MVVM’s components and you can practice the design pattern in small challenges.

If you want to know more about the Google’s components that can help you get the most out of MVVM, read the Android Architecture Component: Getting started tutorial and all the screencasts related to them.

To learn more about Dependency injection you can watch the Dependency Injection with Koin screencast. If you prefer Dagger2, you can read Dependency Injection in Android with Dagger2 and Kotlin. Or watch the screencasts Getting Started with Dagger and Dagger Network Injection.

Even though it’s iOS based, the Design Patterns by Tutorials book covers many of the patterns mentioned in this article.

Finally, design patterns don’t really change much from platform to platform; just the specific code examples and implementation may vary. Be sure to check out the upcoming Advanced App Architecture book for Android for more details on MVP, MVVM, and other architectural patterns on Android.

If you have any questions or comments, please join the forum discussion below!

  • Entities
  • Use cases
  • Interface adapters
  • Frameworks and drivers