« Will 64 bit require in memory databases? | Main

March 03, 2007

The listener pattern evolves #2

So,

I'm thinking about my last post on listeners and I'm thinking so what are the downsides of annotated callbacks.

  • no Java 1.4.2 compatibility
  • I'm forced to use reflection to invoke the methods.
  • No easy way to doc the method signature

These are all issues. Then I think but what about an interface with a single method in it. Instead of defining an annotation, why not just make an interface with a single method. If the listeners wants a specific event then implement the interfaces for each event/method. This gives strong type checking (i.e. the method must be implemented), JavaDoc is easier and I can just invoke the method directly instead of through annotations.

It's not as 'sexy' as the annotation but it's better. The annotation would make sense in addition maybe if I needed compile time arguments to say how to call the event, you'd use annotation modifiers for that. But, this looks like a better approach.

For now, I can't think of why I'd use an annotation over this approach, it looks superior to me from all angles.

March 3, 2007 | Permalink

Comments

Completely agreed Billy.. I think the idea with annotations over interfaces is that it gives you the ability to do fun thing in your annotation implementation class that might be more messy with a straight interface. But you're right.. in the case where you're just trying to flag methods to be called at certain times, the differences are minimal. Except for maybe the customer who's trying to turn a legacy POJO into a persistent class and would love to be able to add an annotation to the legacy method without renaming or 'redirecting' with the implementation method name from the interface.. but that's a small price to pay.

Considering it's not much more from a maintenance and even implementation perspective any ideas on supplying both? The documentation factor seems to be the best argument to NOT do it, but that's why IBM invented info-centers.. javadocs be damned.

Posted by: Rob W | Mar 5, 2007 2:12:58 PM

Post a comment