Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotations for design patterns?

Is there a project that maintains annotations for patterns?

For example, when I write a builder, I want to mark it with @Builder.

Annotating in this way immediately provides a clear idea of what the code implements. Also, the Javadoc of the @Builder annotation can reference explanations of the builder pattern. Furthermore, navigating from the Javadoc of a builder implementation to @Builder Javadoc is made easy by annotating @Builder with @Documented.

I've being slowing accumulating a small set of such annotations for patterns and idioms that I have in my code, but I'd like to leverage a more complete existing project if it exists. If there is no such project, maybe I can share what I have by spinning it off to a separate pattern/idiom annotation project.

Update: I've created the Pattern Notes project in response to this discussion. Contributions welcome! Here is @Builder

like image 245
Greg Mattes Avatar asked Sep 24 '08 14:09

Greg Mattes


2 Answers

This seems like a misuse of annotations to me. Sure, I could see why you might want to note what design pattern a class is helping to implement, but just using the Javadoc and/or the name of the class seems more appropriate. The name of the pattern that you're using is of no actual importance to the code itself... patterns are just a guide for an often used way of solving a problem. A comment would suffice, rather than creating a new file for every pattern you use.

like image 90
ColinD Avatar answered Sep 20 '22 06:09

ColinD


This is an interesting solution, but I keep wondering what's really the problem you're solving with this? Or in other words, what do you get from using something like this what you don't get by a proper comment on top of your class about it's usage?

I can think of a few cons but can't think of benefits apart from this being the nice standardized way to document code.

Cons would be, namely:

  1. one more thing for programmers to think about, which is never a good thing
  2. unannotated patterns might be confusing - someone probably forgot to document it, but maybe it's not a pattern..?
  3. can you really annotate all patterns..? what about patterns which are not tied to a single class/method, for example three-tier architectural pattern, or thread pool, or even MVC?
like image 31
Domchi Avatar answered Sep 22 '22 06:09

Domchi