What is MVP Design Pattern?

MUSTAFA KURT
Huawei Developers
Published in
3 min readJun 24, 2020

What is MVP?

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern which mostly used for building user interfaces. In MVP, the presenter assumes the functionality of the “middle-man”. In MVP, all presentation logic is pushed to the presenter. MVP advocates separating business and persistence logic out of the Activity and Fragment. Model-View-Presenter (MVP) pattern is an android architecture pattern, which for a while now gaining importance and popularity. There are already lots of article already written and published over the internet. Here, I am going to discuss what it is and how easily it can be implemented. There are various ways to implement MVP in your app architecture (Be clear, MVP is not an architectural pattern). To understand MVP, it is better to first define what Model, View and Presenter are.

Model: It is a data access layer for managing data. It can be an interface which is responsible for accessing APIs and local database or cache etc. It can be an interactor (if using Uncle Bob Clean Architecture) or repository (if using Repository Pattern) depends on which type of pattern are you.In an application with a good layered architecture, this model would only be the gateway to the domain layer or business logic. See it as the provider of the data we want to display in the view. Model’s responsibilities include using APIs, caching data, managing databases and so on.

View: It is the only layer for presenting data and reacting to user actions (a button click for example). In case of android, View can be implemented by activity, fragment, widgets or any custom view. This view will contain a reference to the presenter and call a method from presenter.The View, usually implemented by an Activity, will contain a reference to the presenter. The only thing that the view will do is to call a method from the Presenter every time there is an interface action.

Presenter: This layer works as a middle-man between view and model. It fetches data from the model layer, format the data and return to the view. It also reacts to user interactions through view interface and update the model.The Presenter is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.

What is MVP Pattern?

MVP pattern separates the presentation layer from the business logic.It draws sharp line between how interface works and how we show it and decouples the two.

Why to use MVP?

  • It maximize the amount of code that can be tested with automation.
  • It separates business logic from the view logic to make the code easier to test and understand.

Need of MVP in Android

In android, components are closely coupled to life-cycle of the app. For example, activities are coupled to data access mechanisms and it is also bound to the life-cycle. To make an app maintainable and testable, it is very important to keep layers separated and this is where MVP helps. It decouples the view from data source. For example, if you change your data source, you don’t need to make any change to the views.

Structure of MVP

Let’s practice MVP logic in our code. A marker identifies a location on a map. Basically marker uses 2 parameter. Title and position info(latitude,longitude) . We will create model-view-presenter logic for these 2 parameters.

Please reference Sample MVP Code by below link;

Thanks for you have read this article.

I hope it will be helpful.

Hope to see you in next articles.

Until that time, bye.

--

--