Multiple dispatchers

You might wonder, why the methods of the EventListener class cannot be invoked statically, which could save you the calls to getInstance() everytime you need to trigger an event or add a listener.

This approach has been taken, to be able to allow more than one dispatcher instance per application. The getInstance() method accepts a name for the dispatcher as its sole parameter, so you can easyily create as many dispatchers as you need and still easily identify them:

import net.schst.EventDispatcher.*;

// all events related to users will be handled by this dispatcher
EventDispatcher userDisp = EventDispatcher.getInstance("user");

// all events related to debuggin will be handled by this dispatcher
EventDispatcher debugDisp = EventDispatcher.getInstance("debug");

To add listeners you now only need to know, which dispatcher is responsible for the events you need to handle.

In web applications, you can also use this to create a new dispatcher for each session of your application.