Use singleton EventBus in README and unit test

This commit is contained in:
Kai S. K. Engelbart 2020-09-08 09:38:46 +02:00
parent f407021cea
commit bdf7dcfeda
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 7 additions and 7 deletions

View File

@ -8,6 +8,8 @@ Any class can be made an event by implementing the `IEvent` interface.
Using an instance of the `EventBus` class, an instant of the event class can be dispatched. Using an instance of the `EventBus` class, an instant of the event class can be dispatched.
This means that it will be forwarded to all listeners registered for it at the event bus. This means that it will be forwarded to all listeners registered for it at the event bus.
In addition, a singleton instance of the event bus is provided by the `EventBus#getInstance()` method.
To listen to events, register event handling methods using the `Event` annotation. To listen to events, register event handling methods using the `Event` annotation.
For this to work, the method must have a return type of `void` and declare a single parameter of the desired event type. For this to work, the method must have a return type of `void` and declare a single parameter of the desired event type.
Additionally, the class containing the method must implement the `EventListener` interface. Additionally, the class containing the method must implement the `EventListener` interface.
@ -30,13 +32,12 @@ import dev.kske.eventbus.*;
public class SimpleEventListener implements EventListener { public class SimpleEventListener implements EventListener {
public SimpleEventListener() { public SimpleEventListener() {
EventBus eventBus = new EventBus();
// Register this listener at the event bus // Register this listener at the event bus
eventBus.register(this); EventBus.getInstance().register(this);
// Dispatch a SimpleEvent // Dispatch a SimpleEvent
eventBus.dispatch(new SimpleEvent()); EventBus.getInstance().dispatch(new SimpleEvent());
} }
@Event @Event
@ -65,7 +66,7 @@ To include it inside your project, just add the Maven repository and the depende
<dependency> <dependency>
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId> <artifactId>event-bus</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
</dependency> </dependency>
</dependencies> </dependencies>
``` ```

View File

@ -12,17 +12,16 @@ import org.junit.jupiter.api.*;
*/ */
class EventBusTest implements EventListener { class EventBusTest implements EventListener {
public EventBus eventBus = new EventBus();
int hits; int hits;
@BeforeEach @BeforeEach
public void registerListener() { public void registerListener() {
eventBus.registerListener(this); EventBus.getInstance().registerListener(this);
} }
@Test @Test
void testDispatch() { void testDispatch() {
eventBus.dispatch(new SimpleEvent()); EventBus.getInstance().dispatch(new SimpleEvent());
} }
@Event(priority = 50) @Event(priority = 50)