Event handling is something very common, especially in desktop application. So how does you’re event handling code look like? Something like this?
Of course you don’t want to have this kind of code in every class. Therefore you create a special event-handling class. That class manages all registered events and can fire the event to all listeners.
But how do you do that when there are multiple event types? Then the types are represented by different interfaces with different method signatures. You really want to handle all these events with the same event-handler class. Let’s take a look at my solution. Here’s an example where I handle different events:
As you see I use the EventListeners for representing an event. To register events I just call the add-method on it. Now to the real cool part: To fire the event, I get the invoker and just call the method I want to. That method can by any method of the event-handler interface. That makes it very flexible; I can use this for any kind of event-handler interface! I haven’t seen a more elegant way to handle events in Java yet.
How is this implemented? The internally implements the usual add/remove and invoke-logic, but with a twist. It uses the generic argument for handling everything. For the invoke-part it creates a dynamic proxy object of the generic argument. That is the object which is returned by the invoker()-method. When you call any method on that object, it will delegate that invoke to all listeners. (Yes, for the .NET guys, that somewhat a reimplementation of a multi-cast delegate).
For my implementation I just used a Java proxy instance. Of course the implementation adds overhead to the call and only works with interfaces. For a more advanced implementation you could use a code generator library. That probably would speed up things and also allow classes as event-handlers.
That’s it. I handle my event this way in Java. If you found an event more elegant way, let me know.
P.S. I’m wondering if I should start a small Java library which contains all these kinds of small tricks I’m posting here. Hmm…