Blackboard
has been updated again!
I’ve added some magic recognition code in there, to allow people to skip the
blackboard.register(listener, event);
routine, allowing Blackboard to automagically scan for existing Event/Listener pairs.
First of all, I’ve added a new annotation – @ListenerPair – which can be given to your Event class. It takes the corresponding Listener interface as its argument. This tells Blackboard how the listeners and events should be paired together. You don’t have to use the annotation, though, if you follow a new convention:
Write each Listener as a public inline interface to its corresponding Event
. Example:
@ListenerPair(MyListener.class) // this is not necessary, in this inline case
public class MyEvent implements Event {
public interface MyListener implements Listener {
@ListenerMethod
public void react(Event event);
}
}
So, if your events and listeners are structured this way, you don’t
have
to use the @ListenerPair annotation (although it’s recommended, to make it explicit).
The automatic registration can be done in one of two ways:
1)
As soon as you have instantiated the Blackboard instance, call
blackboard.discover()
. This sends Blackboard to an epic journey of magic code, which tries to find all listener/event pairs (according to the format mentioned above)
in and underneath the current package
. You can modify which package acts as a root, by using
blackboard.discoverFrom(Class)
instead, where Class is any class in the desired root package. This should work most of the times, but since it’s a bit magic, it can fail terribly. YMMV
2)
Skip the “discover()” call, and just don’t do anything. Each time you add a listener to Blackboard, that will be paired with a suitable Event just in time. This format
requires
the code style convention (with the inline interfaces), and annotations will not help you here.
So, here’s some lazy coding improvements. You are still allowed to register each pair manually (which will disable any magic thingies, by the way), and that’s always the most reliable way to register them. I have some ideas about 2.2.0, but those might need to wait for quite some while.