tags:

views:

112

answers:

2

I'm reimplementing a .Net API in Java and the API specifies a whole bunch of events, ehich java doesn't implicitly support.

I'm going to use an observer pattern but due to the number of event I really don't want to clutter the interface.

I was wondering if it was a good idea to declare an "Event" class which has a subscribe method which takes an "EventHandler Interface" and a throw method.

this way I'm not cluttering my parent class with umpteen lists of subscribers because the individual events handle them.

The only issue I can see is with the arguments for the throw command, as different events would have different arguments.

The solutions I've come up with are letting the throw method accept an array of objects or, creating an interface like IEventArguemnts which can be passed to the throw command and handled by the code that has subscribed to the event, this seems to make more sense to me.

I'd appreciate any suggestions for improving this. Or any refinements.

+2  A: 

Java has events, and also API support for events. Check out the java.util package. You'll see java.util.EventListener, java.util.EventObject and some others. There are also support classes for maintaining subscribers, etc. The AWT and Swing APIs for example are heavily event-based.

Normally the addXxxListener(XxxListener l) methods (i.e. the subscription methods) would go on the class that's firing the events, not on the event class itself. The event is the message and it's what's being fired from publisher to subscriber.

Willie Wheeler
Right, you don't need Observer if you use this. That would be my recommendation as well.
duffymo
My logc behind creating a separate Event class was that the logic for maintaining the subscribers and notifying them could be delaged to the event class. I'll have a look at the classes you've mentioned and see if they do what I need them to.
Omar Kooheji
Have a look at the java.beans.PropertyChangeEvent, java.beans.PropertyChangeSupport and java.beans.PropertyChangeListener. They might be all you need if everything's in one JVM.
duffymo
IF you put the subscribers in the event, then you end up passing the list of subscribers around to the listeners. In general they don't use that. Possible security issue there too.Analogy: If you're a mail order retailer, where do you maintain your mailing list? Not usually in the catalog...
Willie Wheeler
...to give an example of the security issue, suppose a supplier of "intimate adult apparel" were to manage its mailing list by including it in the product catalog. Anyway the security issue isn't the key one. It's keeping the concerns separate. The event represents the message, not the registration.
Willie Wheeler
No, the subscribers don't go in the Event. The PropertyChangeSupport class is added to the JavaBean that wants to notify Listeners that its state has changed by firing an Event. That's the object that maintains the list of subscribers.
duffymo
A: 

Why not just use the JavaBeans event model? If all the objects are running in the same JVM, that will work fine.

Peter Coad had some thoughts about improving the Java Observer model. Perhaps you'll find these helpful.

duffymo