Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If there is a specific listener interface for each event. Each event is able to issue listeners calls itself. Then, the role of the dispatcher is to identify target listeners and to trigger the event notification on them.</p> <p>For example, a generic event definition can be:</p> <pre><code>public interface GameEvent&lt;L&gt; { public void notify( final L listener); } </code></pre> <p>If your CollisionListener is:</p> <pre><code>public interface CollisionListener { public void spaceshipCollidedWithMeteor( Spaceship spaceship, Meteor meteor ); } </code></pre> <p>Then, the corresponding event can be:</p> <pre><code>public final class Collision implements GameEvent&lt;CollisionListener&gt; { private final Spaceship ship; private final Meteor meteor; public Collision( final Spaceship aShip, final Meteor aMeteor ) { this.ship = aShip; this.meteor = aMeteor; } public void notify( final CollisionListener listener) { listener.spaceshipCollidedWithMeteor( ship, meteor ); } } </code></pre> <p>You can imagine a dispatcher which is able to propagate this event on the target listeners like in the following scenario (Events is the dispatcher class):</p> <pre><code>// A unique dispatcher final static Events events = new Events(); // Somewhere, an observer is interested by collision events CollisionListener observer = ... events.listen( Collision.class, observer ); // there is some moving parts Spaceship aShip = ... Meteor aMeteor = ... // Later they collide =&gt; a collision event is notified trough the dispatcher events.notify( new Collision( aShip, aMeteor ) ); </code></pre> <p>In this scenario, the dispatcher did not require any knowledge on events and listeners. It triggers an individual event notification to each listener, using only the GameEvent interface. Each event/listener pair chooses it's own dialog modalities (they can exchange many messages if they want).</p> <p>A typical implementation of a such dispatcher should be something like:</p> <pre><code>public final class Events { /** mapping of class events to active listeners **/ private final HashMap&lt;Class,ArrayList&gt; map = new HashMap&lt;Class,ArrayList &gt;( 10 ); /** Add a listener to an event class **/ public &lt;L&gt; void listen( Class&lt;? extends GameEvent&lt;L&gt;&gt; evtClass, L listener) { final ArrayList&lt;L&gt; listeners = listenersOf( evtClass ); synchronized( listeners ) { if ( !listeners.contains( listener ) ) { listeners.add( listener ); } } } /** Stop sending an event class to a given listener **/ public &lt;L&gt; void mute( Class&lt;? extends GameEvent&lt;L&gt;&gt; evtClass, L listener) { final ArrayList&lt;L&gt; listeners = listenersOf( evtClass ); synchronized( listeners ) { listeners.remove( listener ); } } /** Gets listeners for a given event class **/ private &lt;L&gt; ArrayList&lt;L&gt; listenersOf(Class&lt;? extends GameEvent&lt;L&gt;&gt; evtClass) { synchronized ( map ) { @SuppressWarnings("unchecked") final ArrayList&lt;L&gt; existing = map.get( evtClass ); if (existing != null) { return existing; } final ArrayList&lt;L&gt; emptyList = new ArrayList&lt;L&gt;(5); map.put(evtClass, emptyList); return emptyList; } } /** Notify a new event to registered listeners of this event class **/ public &lt;L&gt; void notify( final GameEvent&lt;L&gt; evt) { @SuppressWarnings("unchecked") Class&lt;GameEvent&lt;L&gt;&gt; evtClass = (Class&lt;GameEvent&lt;L&gt;&gt;) evt.getClass(); for ( L listener : listenersOf( evtClass ) ) { evt.notify(listener); } } } </code></pre> <p>I suppose its fulfills your requirements:</p> <ul> <li>very light,</li> <li>fast,</li> <li>no casts (at usage),</li> <li>Every thing is checked at compile time (no possible mistake),</li> <li>No API constraints on listeners (each event choose it's own messages),</li> <li>Evolutive (no dependencies between different events and/or listeners),</li> <li>The dispatcher is a generic black box,</li> <li>Consumers and producers don't need to know each other.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload