I am trying to separate some UI code using interfaces and events, and I would like to know if there is a way / best practice in Java for declaring an event as part of a Java interface - something like what C # provides:
// C
My goal is simply to mark the interface so that it is known that it (the developers) fires events of a certain type. I hope that I can include more than documentation just to enforce this behavior. I understand that Java does not provide the keyword βeventβ or a direct way to do this, so I hope for some tips on a workaround or best practice for achieving this.
One way I can do this is to create a universal marker interface that provides the ability to fire events:
public interface FiresEvent<T extends Event> { public void fireEvent(); }
... and then inherit this interface in my user interface.
public interface MyInterface extends FiresEvent<ActionEvent> { }
The problem with this approach is that a FiresEvent can only be inherited once, even if the generic type is changed, so the solution does not seem generic enough for the case when an object can be the source of several events.
I would be interested to know how people deal with this, and if there is a better way than just documenting the need to trigger events.
EDIT: Maybe the following question will clarify my question:
I understand the Java handler list approach and do it yourself by letting callers register object handlers. In my case, I rely on the event bus, so we can view the "shooting" events as placing them there, to redirect something else to the handlers.
I would like to define the responsibility of the interface as:
- An interface designer fires an event of type T on the world / event / anything bus
- An interface designer should not delegate to the most registered users.
source share