MVC vs MVP vs MVVM architecture in Android

MVC vs MVP vs MVVM architecture in Android

Here is the blog to focus on the Architecture of Android which is very important to be a perfect coder. In this blog, we will see MVC vs MVP vs MVVM.

What is Architecture?

If you are building an application in an organized manner with some set of rules, describe proper functionalities and implement it with proper protocols, then it is called an Architecture.

Role of Architecture

Let us say if we are not using any architecture and we are writing our code in a class/activity/ fragment in an unorganized manner then the problems we will face are-

  • The number of lines of code will increase that it will become complex to understand.
  • It decreases readability and increases the number of bugs. Thus, it is difficult to test and reduces the quality of the product.

So, to provide clear data flow which will increase robustness, scalability, bug resistant, increase readability, easy to modify and increase productivity and provide a quality app. Thus, we should use proper architecture, suitable to work in a team.

Some principles for good Architecture in Android

To get good architecture there are some basic concepts we should follow. They are:-

  • Separation of concern: Component should do what it is required. Shown in the diagram.
MVC vs MVP vs MVVM architecture in Android

This we can achieve by Architecture pattern .

  • No Hard dependency: It should be fixed if every component should work on some limited amount of dependency. All dependencies should be provided from outside. Use Dependency Injections. Here are the links-

Dependency Injection and Dagger2 basic concepts

Dagger2 Advance concepts

  • Manage lifecycle and data persistence : It can be achieved by Architecture Component. (We will read about Architecture Component in upcoming blogs)

Architecture pattern:

Software Architectural Design pattern promotes organized programming. Separates application functionality which is easy to test and provides low-cost maintenance. MVC, MVP, MVVM are some popular architecture patterns.
Let’s explore them, one by one:

MVC:

It is a Model-View-Controller. The most commonly used architecture. These are the three components used in MVC.

Model - It is business logic and Data State. Getting and manipulating the data, communicates with the controller, interacts with the database, sometimes update the views.
View - What we see. User Interface consists of HTML/CSS/XML. It communicates with the controller and sometimes interacts with the model. It passed some dynamic views through the controller.
Controller - It is Activity/Fragment. It communicates with view and model. It takes the user input from view/REST services. Process request Get data from the model and passes to the view.

As shown in the figure:

MVC vs MVP vs MVVM architecture in Android

Advantages

  • It keeps business logic separate in the model.
  • Support asynchronous techniques
  • The modification does not affect the entire model
  • Faster development process

Disadvantages

  • Due to large code controller is unmanageable.
  • Hinders the Unit testing
  • Increased Complexity

Here are some examples where it is used- RubyOn Rails, Codeigniter, Angular, Django, etc. You can find out code sample for MVC: MVC Sample code

MVP:

It as Model-View-Presenter. For the phase of developing time or for the phase of developers it is vital to divide the architecture into layers. It breaks the dependency on what we have on view.

Model - It is business logic and Data State. Getting and manipulating the data, communicates with the presenter, interacts with the database. It doesn't interact with the view.
View - Consists of UI, activity, and fragment. It interacts with the presenter.
Presenter - It presents the data from the model. Control all the behavior that want to display from the app. It drives the view. It tells view what to do. Any interaction between the model and the view is handled by the presenter. Saves the data back to the model.

As shown in the figure:

MVC vs MVP vs MVVM architecture in Android

Advantages

  • It makes view dumb so that you can swap the view easily.
  • Reusable of View and Presenter
  • Code is more readable and maintainable
  • Easy testing as business logic separated from UI

Disadvantages

  • Tight coupling between View and Presenter
  • Huge amount of interfaces for interaction between layers.
  • The code size is quite excessive.

You can find out code sample for MVP: MVP Sample Code

MVVM:

It is a Model-View-ViewModel. It losses the tight coupling between each component and reduces the glue classes. Works on the concept of observables. Children don't have reference to the parent, they only have reference by observables.

Model - It has business logic, local and remote data source and repository.Repository: communicate with local or remote data sources according to the request from ViewModel.
View - Only user interaction i.e.XML, no business logic. Direct send user action to view model but does not directly get a response. To get a response view observes some data which ViewModel exposes.
ViewModel - Most of the user interface logic center it here. It is a bridge between a view and a business logic. It does not have any clue which view has to use it. As it does not have a direct reference to the view. Thus, good in testing and has loose coupling. But still, it needs to update the UI this interaction done by observables. When data changes observable notifies.

As shown in the figure:

MVC vs MVP vs MVVM architecture in Android

Advantages

  • No tight coupling between the view and view model
  • No interfaces between view and model.
  • Easy to unit testing and code is event-driven.

Disadvantage

  • You have to create observables for each UI component.
  • The code size is quite excessive.

You can find out code sample for MVP: MVVM Sample Code

I tried my best to give you a clear picture of the architecture pattern. I hope it may help you.
Do share this blog with your fellow developers to spread the knowledge.

Happy Learning!