Question

I have a presenter which makes calls to Handler and get the data from server same data is needed for another widget which is a miniature version of the existing view, but this will present all time in application.

Here what i have common is calls to handler, same handler and action objects.

What is the best way to design.

My possible solution : 1) Writing one Common class which has access to the dispatcher object (Inject through Ginjector), use the methods to get data. But as per MVP architecture, dispatcher usage is restricted to presenter only, but this is non-presenter class.

Was it helpful?

Solution

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:

  1. 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.
  2. 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.

OTHER TIPS

If you are using event bus, your presenter(which makes calls to Handler) can fire events with new data and your miniature widget can register with event bus to receive them. This way only one presenter would call to server and anything on client can be notified using events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top