AFAIK there is nothing about MVP
which says that everything has to be done in the Presenter
. It make sense to encapsulate logic which is common to multiple Presenters
in one common class.
MVP
is rather a pattern than a rule which is written in stone. So when it makes sense you can deviate a little bit from the pattern.
IMHO the common class is a the right approach.
Using a common class for handling the requests to the backend also makes it easy to implement caching and authentication for example.
There are two ways to do the communication between your Presenters
and the common class:
- Inject a singleton instance of your common class into all the
Presenters
that need the data. From the presenter you can call the a method on the common class and register a callback.
- Inject the global
EventBus
into the common class and fire an Event
(i.e. LoadDataEvent
) from the corresponding Presenters
and handle this Event
in the common class. When the data is received from the backend you can fire another Event
(i.e. DataLoadedEvent
) from the common class and handle it in the corresponding Presenters
.
Solution 1 is probably easier to implement but you have some coupling between the common class and the Presenter
(it's not that bad if you use dependency injection).
Solution 2 requires a little bit more code (you have to define the Events) but provides a lot of flexibility and de-coupling. For example if you create a new Presenter
that is also interested in the data you just need to register a Handler for the DataLoadedEvent
in the Presenter and you are done.