Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android architecture components with MVP

New Android Architecture Components released on google IO 17.

So should we use MVP with architecture components and MVVM?

In google sample on Github, they have used the MVVM model. Google Sample

like image 472
Jarvis Avatar asked May 28 '17 19:05

Jarvis


People also ask

What is MVP architecture in android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What is MVP and MVVM in android?

MVVM architecture goes well with android applications and updates the software with the new patches in the system. This helps the software to be up-to-date. MVP uses different libraries to help users who use the architecture. MVVM follows a pattern and is forecasted to evolve as a common tool used by many.

Which is better MVP or MVVM?

MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing. All my projects(written in Kotlin for Android app) are based on MVVM.


2 Answers

About the selection of architectural pattern and its implementation, it always depends on many factors such as team members, app's types, and so on.
In Google's blog post, they have already said that Architecture Components are mainly

Opinions not Prescriptions.

However, three main components (Room, ViewModel, ViewData) are developed with wide-range usage but not focus on any specific pattern.
For example,

  • If you are using SQLite to persist local data in your app, you can you Room no matter what kind of pattern you are using, it is really a great library with a lot of benefits such as reducing boiler plate codes, validating SQLite syntax in compilation time, etc.
  • Besides, LifeCycle, LiveData, and ViewModel have their own strong points. Especially, they address lifecycle-handling issues really well.
  • LiveData gives you one more tool of observer pattern which handles data stream reactively. (Like a great feature RxJava provides us).
  • ...

At the end, for your question.

So should we use MVP with architecture components?

The answer is "it's nice part of Android framework, so why not".

Updated:
If you want to see how a MVP pattern project works with new Architecture Components, please checkout my repository on Github. In which I enhance previous MVP model by using Room for local persistence and LifeCycle to create lifecycle-aware Presenter.

like image 126
Quang Nguyen Avatar answered Sep 20 '22 02:09

Quang Nguyen


I'm currently building a template project that uses MVP pattern along with Google Architecture components (Room, LiveData), and compare to mix version of Realm and LiveData:

https://github.com/duyp/mvp-template

I think some of Google Architecture Components are compatible with MVP Pattern, not only for MVVM :D

You can check out 2 following branches:

  • realm_livedata: MVP with Realm and LiveData

  • room_livedata: MVP with Room persistence library and LiveData

It's much more complex than Google sample project (I used Dagger 2, RxAndroid, Retrofit, Gson, and clean architecture) hope you find a better solution for your work. If any question or suggestion, you can leave comments or issues on my github project.

I also created a project using MVVM with new Google architecture components with Realm, Dagger 2, Live data: https://github.com/duyp/mvvm-template

Happy coding!

like image 39
Duy Pham Avatar answered Sep 21 '22 02:09

Duy Pham