views:

357

answers:

4

Hello everybody,

I want to implement a design in Java where I have multiple event sources (Threads). Such event source accomplish a specific task and had to notify the unique Event Handler (Class) and this one have to accomplish other tasks according to event sources notifications.

My question is : how to implement this desiqn in the appropriate manner in Java? There is a design pattern similar to this design?

Thank you in advance :).

+1  A: 

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 singleton
class EventHandler implements Observer {
 public synchronized void update(Observable o, Object arg) {
  accomplishOtherTask();
 }
}
Justin
Thank you Justin for your response.I tryed this solution , but the problem was in Threads (who are considered as Observable) because my threads already extends from another class, and If you want to use Observable you must extend from it , and as you know multiple inheritance is not allowed in Java.Have you any suggestion?
Zakaria
Your Thread can include an Observable or you could have your base class extend Observable. Using runnable (instead of extending Thread) is preferred for this among other reasons.
Justin
Thanks !I understand your Idea and I implemented it.But I really don't understand your code sample: First we can not implements Observable we must extends it. Probably you mean Observer.If this is right your code sample is fine.
Zakaria
Ah, yea fixed it. They probably should have name the pattern parts Observer and (something else that doesn't look the same: ObservationProducer)
Justin
A: 

Hi,

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.

Arjan Molenaar
Thank you Arjan Molenaar for your respnse.I will google for Actor frameworks in Java, can you give me further explanations?
Zakaria
A: 

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

Glenn
Thank you for your response.Can I use those implementations for non GUI events??
Zakaria
A HandlerManager is just a queue of observers. When an event is fired on the HandlerManager, the manager will check each observer if it can respond to the event. if it can, then the overserver will be called with the proper args and so on.
Nick Hristov
Yes, Zakaria, java.beans.PropertyChangeSupport can be used for non-GUI events.
Glenn
A: 

Maybe I don't understand your question, but I think you don't need any design pattern, but something from the java.util.concurrent package.

A ThreadPoolExecutor ?

mexique1