Better singleton aspect [message #1034484] |
Fri, 05 April 2013 09:39  |
Eclipse User |
|
|
|
The following is a neat Java singleton pattern:
public class XXX {
private static XXX instance = new Singleton();
private XXX () {}
public static XXX getInstance() {
return instance;
}
}
I hoped that I could implement an 'aspect' which can be applied to singleton classes in order to avoid this overhead. However, I am unable to find or implement a good solution. Google can only find the following one: http://www.christianschenk.org/blog/singletons-with-aspectj/
But honestly, "new XXX()" for getting the singleton instance? This solution can also be found in the AspectJ cookbook. IMHO this is a no go! There is also a quote:
Quote:The presented implementation should be well documented, because nothing would be more irritating than a novice developer in your team who doesn't know about this magic behind the scenes and relies on different instances of the singleton class ("I thought new would be doing...").
I cannot believe that the trivial singleton code above cannot be weaved into some classes (e.g. with MySingletonAnnotation). Is there no solution which allows the writing of "XXX.getInstance()"?
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03042 seconds