Android: MVC, MVP, MVVM

28 / Jan / 2016 by Shalini Prajesh 9 comments

In this blog, Model View Controller (MVC), Model View Presenter (MVP) and Model View View-model (MVVM) are briefly discussed. We’ll see… what are these design pattern? What impact do they have in our daily coding habits?What are their different use cases? How we can implement these in our Android projects in order to reduce the code complexity in order to make it intelligible. Before moving forward I would like to clarify that all discussion is made in context of Android as a platform. Lets have a look on each pattern one by one:-

MVC(Model View Controller)

I think this is the most widely used approach in Software Development. Model View Controller consist of three main components, around which the whole architecture is revolving.

View:- This component directly interacts with user and is responsible for how user going to see our application. In MVC, Xml is treated as view.

Model:- Model is the data Source for the Application and the main business logic is defined here, it contains data objects that are used in application and is shown to user. Data Source can be Web, local database(sqlite) etc.

Controller:- Here comes the important part of MVC pattern, Controller is the component that manipulates, edit, uses data model and show it to users via View. Controller is responsible for gathering all data and act as middle men between model and view. Activity/Fragments are considered to be Controllers in Android.

All these components interact with each other and perform their specific task. As shown in figure see their interactions among themselves.

1*U6JRenliQAVEsdD7YZuv1g

It has been noticed that Android is not able to follow the MVC architecture completely, as Activity/Fragment can act as both Controller and view, which makes all the code cluttered at one place. Activity/Fragment can be used to draw multiple views for single screen in app, thus all different data calls and views are populated at the same place. Therefore to solve this problem, we can use different design pattern or can implement MVC carefully by taking care of conventions and following proper programming guidelines.

MVP (Model View Presenter)

Model View Presenter (MVP) is derived from MVC pattern. It is emerging and becoming popular amongst developers. MVP is somewhat able to minimize high dependency on View as it separates view layer from business logic layer by presentation layer. Presentation layer is decides what to be displayed on view. Lets discuss its component individually,

View:- It renders information to users and contains UI Component .Xml file, Activity, fragments, Dialog comes under View Layer.It do not have any other logic implemented.

Model:- Model here too plays role of domain or business layer and is data source of pattern. It describe the main logic of application and decides from where the data should be fetched.

Presenter:- This layer performs the functionality of Controller and act as middle layer between view and model layer. But unlike controller, it is not much dependent on View. View contact presenter for the data to be presented, then Presenter takes data from model and returns to view in presentable format. Presenter is a simple java class that do not contain any UI components, it just manipulates data from model and displays it on view.

1*1P4n9JkHChEUVr5umQx4Zw

 

It completely depends on mobile architects and approach to a problem that decides how we can implement this pattern, there are whole lot of ways in which this pattern can be implemented but the main idea to loosely couple view/model interaction should be preserved.

Presenter communicates with view through interface. Interface is defined in presenter class, to which it pass the required data. Activity/fragment or any other view component implement this interface and renders the data in a way they want. Changing views can have different Presenter class. The connection between Presenter:View is one to one.

MVVM (Model View View-Model)

Model View View-Model is introduced in last year’s Google I/O.This architectural plan is becoming popular by the features it provides. It mainly implements the Data Binding Framework, it allows for “binding” of views to fields on an arbitrary object. When a field is updated, the framework is notified and the view is updated automatically. The architecture introduces two-way communication between its components. Along with features like binding, Automatically updating views, it also easy for testing purpose.The functionality of Model and View is same as we have discussed in MVP.

View-Model :- It is responsible for exposing methods, commands, and other properties that helps to maintain the state of the view, manipulate the model as the result of actions on the view, and trigger events in the view itself.View has a reference to View-Model but View-Model has no information about the View.There is many-to-one relationship between View and View-Model means many View can be mapped to one View-Model. It is completely independent of Views.

1*fpTUAtCz8iiWU90WM7Pj4w

 

FOUND THIS USEFUL? SHARE IT

comments (9)

    1. Shalini Prajesh

      In presenter, we have to pass view reference.Passing different kind of views to single presenter increases a lot of work and handling.So I would recommend for each view, a different presenter should be there.

      Reply
  1. Sachin

    Could please check your point “Model is the data Source for the Application and the main business logic is defined here, it contains data objects that are used in application and is shown to user. Data Source can be Web, local database(sqlite) etc.”

    I think you can’t write business logic in model. Model act as a gateway to the domain layer or business logic or act as the provider of the data we want to display in the view.

    Reply
    1. Shalini Prajesh

      Model is a concept that defines the business logic.While writing code it’s up to you and your application’s requirement that how many data source(the web or local DB) it can have.Based on which, you can separate the flow that reaches to data source but your selection of which data source to be selected is triggered from your model.For example: – If we take MVP, let’s see how Login feature can be divided in Model View and Presenter.
      LoginActivity(View)—>LoginPresenter—>LoginInteractor
      Here, my LoginInteractor is doing the role of model as it is doing the login operation.Now further, if you have different data source then LoginIneractor is a place from where that flow will trigger.

      Reply
  2. Dhara

    Hi, can you please provide some github links for the three that you have just explained so that it is easy to know how it all goes in code? I am asking this as i am new to all of this and i would want to start using any one of these patterns.

    Thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *