I\'m working on a little Java game in which all sorts of events can happen. There are at least a couple of dozen basic events that various event handlers may be interested in.
Java beans should have had this interface: it makes life simpler.
interface PropertyChangeProvider {
void addPropertyChangeListener(PropertyChangeListener l);
void addPropertyChangeListener(String property, PropertyChangeListener l);
void removePropertyChangeListener(PropertyChangeListener l);
void removePropertyChangeListener(String property, PropertyChangeListener l);
}
Implement it all over the place.
Make a blackboard class (probably a singleton. this is a sketch only)
public class Blackboard implements PropertyChangeListener,PropertyChangeProvider {
static Blackboard getInstance(){
// implement this
}
void initialise(){
// start the thread here
}
void republish(){
// this can save you heartache too.
}
}
Give Blackboard a thread, listen for events and republish using its own thread.
Classes can just publish their events to the blackboard.
Subscribe to the blackboard for events.
If you want you can persist events, allow republishing etc.
For something inside an app it is very good. (works just as well as a data interchange interface as well!)