What kind of high-falootin' title is that!? Don't try to come off all educated now...
Any-road, I have been leading the dual Java/.NET existence for the last few months, and I realized that Java 5 has Annotations, which they obviously stole from you-know-where.
Since I am a great fan of Inversion-of-Control, I took the 10 minutes to read about how to define and use Java Annotations.
The beauty of marking stuff with meta-data is just that: beauty. I can now apply whatever algorithms (i.e. "protocol") I want, and I can control participation simply by checking for the meta-data.
This is important, because otherwise the "burden" of the protocol gets uninverted back into the instance itself, whereby I have to remember to deposit all these bits and baubles of code in a bunch of overriden methods, and heaven forbid if I want to alter the protocol, because now I have all these instances I have to tweak, and hope I get it right 100%.
Another advantage is similar to using interfaces, in that any instance participating in the annotation protocol is not obligated to derive from anything in particular.
I am currently using annotations to produce a UI binding framework for a certain mobile platform, whereby the "gluing" of code to screen elements is a subtle blend of annotations (to identify the participants and their mappings) which "mark" fields that contain (subclasses) of a specific abstract class (it could be an interface).
Without this handy-dandy mechanism, I would be left with (an interface with) bookkeeping methods, at the very least, to build the list of participants the annotations indicate. Not only that, I would have to provide the "extra" data (in the annotation properties) in that code as well.
It's basically the difference between this
public @interface TagMe {
String value();
}
...
public class SomeClass {
@TagMe("LastName") MyInterface field1 = new MyImpl1();
@TagMe("FirstName") MyInterface field2 = new MyImpl2();
@TagMe("FavoriteColor") MyInterface field3 = new MyImpl3();
}
and this
public interface ITagMe {
void add(Map list);
}
...
public class SomeClass implements ITagMe {
MyInterface field1 = new MyImpl1();
MyInterface field2 = new MyImpl2();
MyInterface field3 = new MyImpl3();
public void add(Map list) {
list.put("LastName", field1);
list.put("FirstName", field2);
list.put("FavoriteColor", field3);
}
}
Only with less freedom. The reason being that it is possible to use multiple annotations, in order to specify additional or optional meta-data to the protocol, or to a (possibly unrelated) protocol that is using the same set of participants.