How does the Guava EventBus work?
A) It allows for decoupled event publishing and subscription
B) It handles database transactions automatically
C) It manages HTTP requests in a web application
D) It provides a GUI framework for Java applications
Answer:
A) It allows for decoupled event publishing and subscription
Explanation:
The Guava EventBus
allows for decoupled event publishing and subscription, enabling components to communicate with each other through events without requiring direct references. It simplifies the observer pattern by providing a simple API to post events and register subscribers.
For example:
EventBus eventBus = new EventBus();
eventBus.register(new EventListener());
eventBus.post(new Event());
In this example, the EventBus
is used to post an event, and any registered subscribers that are interested in that event will be notified and can handle it. This promotes loose coupling between components in an application.