Adapter, What Responsibility is it In MVP Pattern? Data? View?

In Android, Adapter is so important.

Android Application has so much repetitive View. For efficient management, it should use recyclable view and show view only on screen.

So Adapter define repetitive View and we manage Data. it is so easy for implementation of repetitive view.

Regular Adapter

In Messanger App or something like that, it often occured Data’s Add-Remove-Modify. but We just change data and then notify to Adapter for updating.

Like this code, Adapter manage Data and refresh view.

but it is conflicted Android-Clean-Architecture, especially MVP.

MVP And Adapter

MVP define this each part:

View : it can access real View Object.
Presenter : it is got event from View. and it accomplish Business Logic, request to Model for Database or Network
Model : it can connect to Database or Server and accomplish real logic. it controls data.

Model’s main role is controlling Data.

next code is MVP style.

View(MainActivity) has instance of Adapter. Presenter make datas and send to View. so View refresh AdapterView by Adapter.

Two face Of Adapter

MVP’s Definition is View has component of Real View. but What is Adapter?

is it View? up code is Adapter is close to DataModel. it has ArrayList and controls them.

Adapter has composited Data for Item and View for View.

In Android Framework, it seperate between AdapterView and Data of Item.
It’s pretty good solution. but on Application perspective, it has 2 role, Data and View

Adapter is Divided Two Interface

I define Adapter’s role is Data and View. so Adapter inherits DataModel and DataView. Presenter has Adapter as DataModel. and also View has Adapter but as DataView.

Now, View don’t access Adapter directly. through AdapterDataView, View should refresh ListView(RecyclerView). also Presenter don’t access Adapter directly. through AdapterDataModel, Presenter should control Data.

Conclusion

Adapter was control data and view of ListView(RecyclerView) efficiently. but it has two role, Data and View. so it conflict to Clean-Architecture.

so I’m using Interface for definition of Adapter’s Role. Presenter access only DataModel. also View access only DataView.

In OOP, we have to seperate Object from what role is. since using MVP, I always want to clarify Adapter’s role.

this Sample Code for MVP-And-Adapter:
https://github.com/ZeroBrain/Android-MVP-AdapterRoleSample

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.