The main goal of this project is to explore basic features of EventBus
from guava
.
EventBus
allows publish-subscribe-style communication between
components without requiring the components to explicitly register
with one another (and thus be aware of each other).
Reference: http://www.dmydlarz.com/2017/09/26/event-bus.html
Reference: https://github.com/google/guava/wiki/EventBusExplained
Three types of listeners:
ListenerOne
ListenerTwo
ListenerThree
Three types of events:
ForAllListenersEvent
OnlyForListenerOneEvent
OnlyForListenerTwoAndThreeEvent
Tests for all cases (EventBusTest
).
Using EventBus
is quite straightforward:
- Define event classes.
- Define listener classes with methods for specific event classes:
@Subscribe private void general(Event event) { }
- Create
EventBus
andregister
appropriate listeners:EventBus bus = new EventBus(); ListenerOne one = new ListenerOne(); bus.register(one);
- Publish events:
bus.post(event);
- If we don't need already registered listener, we simply
unregister
it:EventBus bus = new EventBus(); ListenerOne one = new ListenerOne(); bus.register(one); ... bus.unregister(one);