Firstly yes - this is subjective.
I have noticed lately a few libraries seem to be using the let the user tag listener methods in some class as listeners using different annotations to note different events. Infinispan and WELD come to mind as examples of this approach.
WHAT I HATE ABOUT THIS PATTERN
POSSIBLE REASONS
OPINIONS
So why would anyone want to use this approach ? I personally don't get it, it seems to make more work for the implementator, makes life harder for the user, is slower, so why use it ?
Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.
The @interface element is used to declare an annotation. For example: @interface MyAnnotation{}
Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. Annotations start with @ . Its syntax is: @AnnotationName.
The methods annotated by the @Provides annotation is the key to create the instances for injection. It returns the object you want to inject into other components.
Maybe an interesting use case is that the guys from the Servlet 3.0 spec wrestled a lot with this exact problem. The earliest proposals did away with interfaces like e.g. ServletContextListener and relied solely on annotations.
As you indicated, the type safety can be a problem here. It's awkward to guess what the exact signature of a method should be.
For among others because of this reason, the proposal was fiercely shut down by the community and they kept interfaces. Now the required registration in web.xml can be done with annotations, but the definition of the listeners methods are still via interfaces.
Annotations do have their advantages. Fine grained listeners are indeed one thing. You can't always counter this with fine grained one method interfaces. Suppose I have a (listener) interface with 6 methods. Most people only need 1 method, so you decide to break it up in 6 interfaces with each having one method. Now I'm the lucky guy who actually needs all 6 of those. In my class I must now declare I implement 6 interfaces.
I'm pretty sure a lot of people are not going to like that.
With annotations you can always mix and match the exact amount that you need.
Also, with annotations it's trivial to have multiple methods tagged in the same class. You can't do this with interfaces. There is only 1 method that implements a method from an interface, no more and no less.
Yet another 'advantage' of annotations is that since they don't exactly define what they apply to, one can support additional method signatures later. With interfaces you would have to add another interface. In my 6 interface example, suppose you later on decide you also need to support methods with 1 additional parameter, you would need 6 extra interfaces. This can add up fast.
A hybrid solution between interfaces and annotations could be the ability for annotations to specify one of more required method signatures. If such a signature would be defined, the annotation can then only be applied to a method that complies with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With