Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adapter as Presenter? Or talking with a Presenter? Android and MVP

Tags:

android

mvp

I'm trying to follow the MVP pattern. However, I have some doubts on how to handle adapters and view holders on this pattern.

Should I use the adapter as a presenter? Having the business logic?

Or should I pass a instance of the presenter that handles the list logic and then call methods of the presenter when there is any interaction with the adapter elements?

Thank you

like image 269
Fábio Carballo Avatar asked Dec 09 '15 13:12

Fábio Carballo


People also ask

What is MVP in Adapter?

What is MVP? Model View Presenter (MVP) is the latest and greatest Android architecture pattern that helps you decouple business logic (Model) from view logic (Activity / Fragment) by utilizing an intermediate step called the Presenter.

What does MVP stand for in Android?

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.

What is a presenter in Android?

A Presenter is used to generate View s and bind Objects to them on demand. It is closely related to the concept of an RecyclerView. Adapter , but is not position-based. The leanback framework implements the adapter concept using ObjectAdapter which refers to a Presenter (or PresenterSelector ) instance.

How does Model View Presenter work?

Data (model) and UI (view), only communicate with one another through an intermediary (the presenter) . The presenter contains the bulk of the business logic, while the view focuses on how to display the data. The controller responsibility is now split between the view and presenter.


1 Answers

There is no exact/correct definition of implementing MVP in Android

To answer your question, in my view the Presenter should not have any Android logic.

As such, the Adapter would be a "View" then i.e. Presenter provides it the data (via the Activity or Fragment) and it just deals with how to present this.

I'd do MVP as follow.

  1. Model - POJO's, parsing, Storing (SQLlite) and retrieving data (http). Obviously I'd divide the POJO's, parsing and DB logic into sub folders - but this all falls into Model for me.

  2. View - Activity, Fragment, Adapters - Activities & Fragment hold reference to a Presenter that gives them data to display. How this data/messages are displayed, look + feel etc. is dealt with in the View.

  3. Presenter - The Middle man, provides the logic to inputs i.e. Button Clicks, retrieval of data, validation of inputs & then passes the result back to the View (Activity or Fragment)

Here's a great article on MVP

Here's a simplified diagram of MVP

enter image description here

Answer modified from this question (also answered by me)

like image 128
Zain Avatar answered Sep 24 '22 10:09

Zain