Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the MVP pattern, should adapters hold models or should the presenter hold models and have the adapter reference it?

Currently I have it so that an adapter has a reference to all the models in it. But is it better to let the presenter just hold the models and the adapter can simply reference them?

So for example:

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private Presenter presenter;

public Adapter(Presenter presenter){
    this. presenter = presenter;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Model m = presenter.getModels().get(position);
    // bind model to view holder
}

@Override
public int getItemCount() {
    return presenter.getModels().size();
}


}

This way when a Presenter fetches more models, it just simply calls getAdapter().notfiyDataSetChanged(); after the fetch.

like image 960
Sree Avatar asked Jun 23 '16 20:06

Sree


People also ask

What is MVP pattern in Android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What is a presenter 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.


2 Answers

You can really go either way with it. Some would say treat the adapter as part of your view and make it as dumb as possible, but there's definitely a benefit to letting the adapter hold the data if you do it right.

For example, I use an abstract base adapter with generics that holds a list of data objects to drive the recyclerview. It provides all the standard CRUD operations for the list (add, update, delete, move, etc). These methods also handle notifying the adapter of the change, so my client code doesn't have to worry about it. It just hands an object to the adapter or tells it to delete/change one, and the adapter handles the rest.

The big benefit here is a huge reduction in the amount of repeated boilerplate code for CRUD operations and dataset change notifications across the various actors interacting with recyclerviews. If you have more than a screen or two with recyclerviews, this savings adds up quick to make it more beneficial than blindly adhering to a mantra.

like image 79
Josh Kitchens Avatar answered Oct 22 '22 08:10

Josh Kitchens


Normally Adapter considered to be an implementation detail of View.

Presenter should not know View implementation details.

The job of adapter is to hold an array of items and to publish it to views. Adapter should not know about Presenter, models, other views, etc.

Data flow for Adapter, as I understand it:

Model -> Presenter -> View -> Adapter-> ItemView

Control flow is opposite, preferably skipping adapter.

Feel free to ask questions in the project's issues.

like image 24
konmik Avatar answered Oct 22 '22 08:10

konmik