One-to-one going one-to-many

Suppose you have two abstractions – an event listener and an event producer. The relationship is 1:1, that is, there’s one abstraction that fires events, and another one that knows how to deal with them. At some later time it turns out there should be more than one listener. If you’re coding in Java, your programming language goes into your way.
Here is what I mean:

 

Everything is simple and beautiful –  the listener is whatever you need it to be. It can be a mock object,  a logger, UI element, a NullObject. Whatever.

But now it turns out you need both a logger and an UI element. And probably a performance monitor later on. A natural way to do this is  by putting your listeners to the list and calling the method in loop:

 

My problem with such solution is that it requires me to write for-loops all over the place. It’s a boilerplate code repeated all over. Which doesn’t seem elegant.

For functional programming languages, there should be no problems. Even the .NET world has LINQ and delegates.

But Java explicitly doesn’t. Stack overflow hints there are NO way for Java to have delegates. Sun/Oracle suggests using anonymous inner classes instead of delegates which in such cases is unacceptable.

Any suggestions about a way out?

A hand-made solution: the generic ListenerList object based on good old Proxy . The above code would look like this:

 

And this is Java 1.5 compatible, which is good for old code maintenance.

The source code of my hand-made ListenerList is hereby generously released under GLP v3. Behold!

 

 

The ListenerList<T> is a generic utility class, so it saves you code lines. It can be used everywhere the <T> is used: the syntax isn’t any more verbose compared to a plain usage of a <T> class directly. Plus it is null-safe and allows one-to-many relations.

A moral of this story? Every good project must fix some of the language limitations.
3 Responses to “One-to-one going one-to-many”
  1. slicklash

    Well, it’s all nice and so but has drawbacks.
    Why do you assume that all listeners are interested in all events? In most cases they are not.
    Also, event producer knows of the existence of listeners. So, your design is not following “Law of Demeter for Concerns” principle.
    Publish–subscribe pattern would be a better choice.

    • gugit

      The listeners would only implement event handlers for what they actually want to handle. This pattern does not prevent you from grouping listeners into logical groups – like ICarFactoryListeners, IIndustryFacilityListeners, IWorkSafetyListeners etc.

      And yes, event producer must know of existence of listeners so he could fire the events the listeners are suppose to handle.

      You know, it’s like the Button class is implemented in every component-based UI frameworks – every button knows there are clients and so every button is generating ButtonClicked() event.

      That’s the problem of Telepathy Free World – if you don’t tell, you don’t expect it to be known. If you don’t inform your listener, the listener is not aware event has happened.

    • gugit

      “Law of Demeter” does not forbid coupling – it promotes loose coupling. And that’s what producer/listener pattern is all about.

Leave a Reply