- All Known Implementing Classes:
AbstractEventBus
public interface EventBus
This is the interface for an event bus. An event bus is a central place for
ATTENTION:
This interface is designed as a stable API and standard for an event bus. However, there can be various implementations of this interface with different aspects regarding concurrency, polymorphism, performance, etc. While this interface will remain stable, we might change internals of the
NOTE:
The loose coupling makes flows less easy to see, understand and debug. You should only consider this approach for components that should be decoupled by design. Do not get confused by the beauty of the event-bus pattern and avoid using it where straight method calls should be preferred.
E.g. if you have a user-interface with a navigation sub-dialog and various other dialogs they should communicate via
sending
,
listening
to and receiving
events.
The EventBus
allows to communicate between loosely coupled components in a smart and efficient way:
- A component sending events only needs to know the
EventBus
but not the receivers of the events. - A component receiving events only needs to know the
EventBus
but not the sender of the events.
EventBus
and the event itself. ATTENTION:
This interface is designed as a stable API and standard for an event bus. However, there can be various implementations of this interface with different aspects regarding concurrency, polymorphism, performance, etc. While this interface will remain stable, we might change internals of the
EventBus
implementation. Further, you may
want to choose a different implementation of EventBus
when you are inside a front-end application (UI) or a
back-end application (server). Therefore, it is possible to provide your own implementation of EventBus
as a
Java module via ServiceLoader
.NOTE:
The loose coupling makes flows less easy to see, understand and debug. You should only consider this approach for components that should be decoupled by design. Do not get confused by the beauty of the event-bus pattern and avoid using it where straight method calls should be preferred.
E.g. if you have a user-interface with a navigation sub-dialog and various other dialogs they should communicate via
EventBus
to update their views accordingly. However, a business component responsible to read and write
addresses may get the requirement that in case of a change of an address some logic from the domain of another
component should be invoked and that might even reject the change. In the latter case EventBus
is the wrong
choice.- See Also:
-
Method Summary
Modifier and TypeMethodDescription<E> void
addListener
(Class<E> eventType, EventListener<E> listener) This method registers a listener that is interested in events.default boolean
removeListener
(EventListener<?> listener) This method removes a listener.<E> boolean
removeListener
(Class<E> eventType, EventListener<E> listener) This method removes a listener.void
This method sends an event to allsuitable registered listeners
.
-
Method Details
-
sendEvent
This method sends an event to allsuitable registered listeners
.- Parameters:
event
- is the event to send. Technically such event may be any Object such as aString
. However, it is strongly recommended to create explicit value classes named with the suffix "Event". The easiest way to create your own event type is to use a JavaRecord
. Please note that it may seem more easy to use data-objects directly as event, e.g. for the selection of a user, you may send the user object itself as event. However, later you may notice that you also need to send an event if a user is deleted or created and your design and semantic of events will be flawed. Therefore it is strongly to create aUserSelectionEvent
Class
orRecord
containing the selected user or its unique identifier in that example from the beginning to ensure a design for extension and flexibility.
-
addListener
This method registers a listener that is interested in events.- Type Parameters:
E
- is the type of the events to listen to.- Parameters:
eventType
- is theClass
reflecting the events to listen to. Typically this should be the exact type of some event sent viasendEvent(Object)
. However, polymorphic implementations ofEventBus
will also support event inheritance and allow you to register anEventListener
for asuper-class
of an event type.listener
- is theEventListener
that shall benotified
if an event of the givenClass
issend
.
-
removeListener
This method removes a listener. If the listener was notregistered
before this method will have no effect.- Type Parameters:
E
- is the type of the events to listen to.- Parameters:
eventType
- is theClass
reflecting the events to listen to.listener
- is theEventListener
to remove.- Returns:
true
if the givenlistener
has successfully been removed,false
if thelistener
was NOTregistered
.
-
removeListener
This method removes a listener. If the listener was not registered before this method will have no effect.- Parameters:
listener
- is theEventListener
to remove.- Returns:
true
if the givenlistener
has successfully been removed,false
if thelistener
was NOTregistered
.
-