Module io.github.mmm.event

Provides generic, reusable infrastructure to define, send and receive events.

Event


This module provides a minimalistic but powerful API for the event pattern. To get started, here is a simple example how you can define your custom event handler interface:
 @FunctionalInterface
 public interface MyEventListener extends EventListener<MyEvent> {
 }
 
The event itself (MyEvent) can be any object so you can use an interface, class, or enum without any restrictions. Your component sending events can be defined like this:
 public interface MyComponent extends EventSource<MyEvent, MyEventListener> {
   void doSomething();
 }
 
The implementation simply extends from AbstractEventSource inheriting the generic event infrastructure (in case you already have to extend another class, use EventSourceAdapter):
 public class MyComponentImpl extends AbstractEventSource<MyEvent, MyEventListener> implements MyComponent {

   public void doSomething() {
     fireEvent(new MyEvent("Hello World!");
   }
 }
 
Now you already have everything you need for eventing:
 MyComponent component = new MyComponentImpl();
 MyEventListener listener = (e) -> System.out.println("Received event: " + e);
 component.addListener(listener);
 component.doSomething();
 // when you are done, you can unsubscribe the listener
 component.removeListener(listener);