Javid Jamae
Javid Jamae

Reputation: 8999

Should events fire themselves?

I'm not a solid GUI programmer, so I'm trying to understand different event architectures. I'm developing a system (in GWT, but I'm not sure that matters) where we are introducing a few custom events. In general, is it good practice to create an event and have the event fire itself onto to the event bus?

Following some articles and tutorials online, we have our controller code actually firing the events, but then each controller has to duplicate the code to fire the custom event. It seems that if you just put a fire() method on the event itself you can avoid that duplication.

What are the pros/cons of doing this?

Upvotes: 1

Views: 112

Answers (1)

Steve Armstrong
Steve Armstrong

Reputation: 5392

In order to have an event fire itself, you'd need to inject the EventBus instance into the event when you create it. This means your controller (the one newing up the event) would have:

new MyEvent(m_eventBus).fire();

If you rework the code like this:

MyEvent event = new MyEvent();
m_eventBus.fireEvent(event);

then you wouldn't have to put any logic or references to services inside your Event instance, where it's not really needed. If you're using GWT, the HandlerManager class already implements an event bus for you.

Upvotes: 1

Related Questions