简体   繁体   中英

Motivation for Event Bus in GWT

I am creating an MVP-like application in GWT.

  • There are multiple panels, and each one is visible at all times.
  • Each panel has a Presenter, and there is a single AppController which sits above all Presenters.
  • There are some application-level events which arise within one Presenter but have implications for other Presenters.
  • The suggested architecture for this seems to involve an Event Bus. However, I'm not sure I see the advantage over something simpler.
  • In particular, wouldn't it be cleaner to simply allow the AppController (and only the AppController) to subscribe to events from any Presenter? The AppController can then tell each Presenter what to do, given the event.
  • The "Event Bus" seems like a quasi-global variable. But if you can accomplish the same thing with more precisely defined methods (ie the methods which AppController calls on each Presenter) isn't that preferable?

To put my concern more precisely: why introduce an Event Bus at all, rather than simply letting events "bubble up" to the appropriate decision-making level? To me this seems like the most straightforward extension of the MVP concept, and it doesn't require the new idea of an Event Bus. I don't understand what problem the Event Bus was introduced to solve.

The advantage of the eventbus is separation of the code.

You can just fire custom events to the bus and don't need to care about your event anymore. Every presenter subscribes only to that events, which it really needs to know. This will lead into cleaner code because, you don't have to create a dispatcher which has to know all presenter to delegate events to them.

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

Your proposed approach is fine, with one big drawback: it calls for spaghetti code when your app grows.

This presentation is about Android but the arguments hold for GWT too.

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

Finally, this blog post deals with the observer vs. mediator patterns in JS: in GWT, the observer pattern is realized by event handlers whereas the mediator pattern is realized by the event bus. Here's the tl;dr : « use observer “locally”, inside a component, mediator “remotely” between components. »

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM