Java, Design pattern : Multiple event sources and One event Handler Java, Design pattern : Multiple event sources and One event Handler multithreading multithreading

Java, Design pattern : Multiple event sources and One event Handler


I think you are looking for the Observer pattern. Java does have some standard interfaces (java.util.Observer, java.util.Observable), though these are not type specific; so you might consider your own if the domain seems to require it.

class MyThread implements Runnable { Observable observable; public MyThread(EventHandler observer) {  observable = new Observable();  observable.addObserver(observer); } public void run() {  while (!done())  {   Object result = doStuff();   observable.notifyObservers(result);  } }}// might have this be singletonclass EventHandler implements Observer { public synchronized void update(Observable o, Object arg) {  accomplishOtherTask(); }}


Sounds like an actor pattern to me. Each thread acts as an actor, accomplishing one single task. Th eoutcome is set on a queue (yes) to be processed by the next actor.

I have no experience with java actor frameworks, though. Consult Google for that.


In GWT, this is called the event bus. Either GWT.HandlerManager or GWTx.PropertyChangeSupport are Google recommended implementations. The latter is available in J2SE since 1.4.2