Motivation for event bus in GWT

I am creating an MVP-like application in GWT.

  • There are several panels, and each of them is displayed at any time.
  • Each panel has a presenter, and there is one AppController, which is located above all presenters.
  • There are some application-level events that occur within one Presenter, but have implications for other presenters.
  • The proposed architecture for this seems to include an Event Bus. However, I'm not sure that I see an advantage over something simpler.
  • In particular, wouldn't it be easier to just let the AppController (and only the AppController) subscribe to events from any presenter? AppController can then tell each speaker what to do, given the event.
  • The "event bus" seems to be a quasi-global variable. But if you can accomplish the same thing with more precisely defined methods (i.e., Methods that the AppController calls for each presenter), is that not so preferable?

To express my concern more precisely: why introduce an Event Bus at all, and not just let events "rise" to the appropriate level of decision-making? For me, this seems like the easiest extension of the MVP concept, and it does not require a new Event Bus idea. I do not understand what problem the Event Bus was introduced to.

+6
source share
2 answers

The advantage of eventbus is code separation.

You can simply run custom events on the bus and no longer need your event. Each presenter subscribes only to those events that he really needs to know. This will lead to cleaner code because you do not need to create a dispatcher that all presenters must know to delegate events to them.

In my opinion, eventbus is a really good thing to make the code understandable and understandable.

+8
source

Your proposed approach is fine, with one big drawback: it requires spaghetti code when your application grows.

This presentation is about Android, but the arguments remain for GWT.

See also this famous presentation from Google I / O 2009, which explicitly talks about using Event Bus to fight spaghetti code (a must-see if you haven't already).

Finally, this blog post is about observer and mediator patterns in JS: in GWT, the observer pattern is implemented by event handlers, while the mediator pattern is implemented by the event bus. Here tl; dr: "use the observer" locally ", inside the component, the intermediary" remotely "between the components."

+3
source

All Articles