5 Aug 2010 Kay   » (Journeyer)

Events in Java

Java has no integrated event system but most classes in the Java Runtime uses the Listener Pattern for this. This pattern is pretty powerful and (when using Generics) completely type-safe but in the end it is still pain-in-the-ass. Think of a class which wants to send five different events. You have to write five listener interfaces and write at least 15 methods (five register methods, five unregister methods and five fire-event methods) and collections for all the listener objects which are registered. Depending on the type of events you may want to write five event classes which are used as containers for the event data. Lazy programmers therefor put all five events into the same listener interface so they only need one register/unregister/fire tripplet. But this requires the event receivers to implement all five listener methods even if they are interested in only one of them. So some genius invented the Adapter classes which already provide empty listener methods which can be extended by the event receiver. The whole thing is pretty annoying because it's so complicated to implement and to use.

So lets take a look at a different approach...

Continue reading "Events in Java"

Syndicated 2009-06-01 13:23:00 (Updated 2009-06-02 22:23:56) from K's cluttered loft

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!