10th Aug 2022

Which Android Architecture is best : MVP, MVVM, MVI?

Mobile App

Written By, Jalak Kamani

Overview

There are multiple architectures like MVP(Model-View-Presenter), MVVM(Model-View-ViewModel), MVI(Model-View-Intent). Each project has its own architecture requirements. We will be analyzing each of their advantages, disadvantages, usage, and support to which is the best architecture for your project.

What Determines a Good Mobile Application Architecture?

Developers want to achieve below mentioned characteristics in their application architecture.

  • Scalability
    Scalability can be defined as the ability of an application to handle a growing user base without affecting any user experience and performance. It means, scalable architecture supports higher workloads and adapts any effective long term changes without affecting the fundamental changes.
  • Reliability
    Reliability banks on the subject of attaining scalability. If an application is not stable then it’s unreliable and many bugs, crashes, security problems, and customer dissatisfaction can be found.
  • Maintainability
    Maintainability is defined as an easy way to update, modify and maintain your application, so that at maintenance time you don’t unnecessarily waste your valuable time and money. A good Android Architecture helps to reduce approximately 75% of maintenance cost.
  • Code Reusability and Testability
    Code reusability is important if you want to avoid rewriting code and save your time for your project. If you want to simplify your application then you have to choose your architecture according to application functionality.

Now, Let’s describe our architecture in detail.

MVP (Model-View-Presenter)


android-arch-mvp

MVP is the oldest out of these 3 architectures. For testing an application the architecture makes it easy. The presenter is able to represent multiple interfaces and reused many times. There are so many advantages of MVP like modularity, testability and a cleaner and maintainable codebase.

Model : Model is used to store data. It handles business logic and communicates with database and network layers.

View : View is a UI layer(Activity, Fragment, View). View provides user’s action to presenter and Presenter updates the UI of the view.

Presenter : Presenter is between model and view. Presenter takes data from View and adds some business logic and updates the Model After Model change presenter UI in the View according to user Input Action.

Advantage

  • Easy to test.
  • Presenter is reusable.
  • Presenters can be used in a common module.
  • View has few lines of code and it supports visualization.
  • Reduce development costs.

Disadvantage

  • Memory leaks and crashes increase when presenters connect with views.
  • Due to multiple interfaces being used for interaction between layers the code is very complex.

MVVM (Model-View-ViewModel)

android-arch-mvvm

MVVM is a more popular and updated version of MVP. It has some similarities with MVP architecture as the presenter replaces it with a viewmodel. However, some MVP disadvantages are solved by MVVM. It separates two different kinds of logic like presentation logic and business logic. Then it is very useful for making some quick design changes. Let’s discuss its components,

Model : It works as the brain of an app because it manages the data sources. For getting and shaving the data viewmodel and model work together. Two types of data source like Remote data source(retrofit) and Local data source(Room database). It interacts with view models, it sends requests and observes model change callbacks.

View Model : It is linked between View and Model. It transfers data according to the View Request. It has all the information that is displayed in view. The viewmodel needs to update the live data and the view monitors the changes in this live data.

View : It informs the view model that the user performs action. That transmit values into ViewModel and observe some response for updating the UI. It doesn’t contain any application logic.

Advantage

  • Improves the testability of each layer as well as provides code reusability.
  • Enhance maintainability of code and easy to change in project.
  • Same view model can be used for more than one activity or fragments.

Disadvantage

  • This design pattern is not proper for small projects.
  • The logic for data binding is more complex in this architecture as compared to others.
  • It is difficult to reuse code of view and view models.
  • For beginners MVVM architecture is hard to implement.

MVI (Model-View-Intent)

android-arch-mvi

MVI is the newest out of these 3 architectures. It can work with RxJava programming library only. In MVI architecture elements of the layout are divided according to their requirement and they send or receive data. Let’s discuss its components,

User : Other architectures have not added the above mentioned component but MVI does making it quite favourable for android architecturie. It performs some action and observes or reacts in view.

Intent : User performs some action on view and it is received as an input of intent. Then we will send those values by intent to models. Intent is observed by the presenter and translated into a new state by model.

Model : Model is a representation of view. It contains all the information to update the UI. UI state just like Load data, change UI with user requirement, in the model the error is stored as similar to the object. Then it includes everything from loading to list of fetch data.

View : In project requirements Activities and Fragments are implemented in views. They observe intent and get some user-requested response. It is a container that accepts different model states and updates the UI.

Advantage

  • Easy to catch and fix the bug.
  • The state objects are immutable.
  • Easy to test and able to test all layers of application with unit testing.
  • It is a unidirectional and cyclic data flow.

Disadvantage

  • It has Lots of boilerplate code.
  • It creates many objects for all states.
  • This is very costly for application memory management.
Hire Our Modern Mobile App Developers

Develop the Best Mobile App and Rescale
your Business

Get Expert Assistance

So which architecture is best?

All architecture has its advantages and disadvantages. It depends on you and your project task to choose the one among the given options and implement it to your project.

MVP and MVI have some lifecycle problems but it can improve scalability on your project. It creates memory leaks, bugs and crashes issues. MVVM solves this problem but it is heavy on the android architecture component.

Written By,

Android Developer at Yudiz Solutions Ltd