Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » Better singleton aspect
Better singleton aspect [message #1034484] Fri, 05 April 2013 13:39 Go to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
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()"?
Re: Better singleton aspect [message #1041806 is a reply to message #1034484] Mon, 15 April 2013 15:51 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
Because of the singleton pattern's reliance on static methods, there would be no clean solution in an aspect. I agree with you that the solution posted in the blog is not what I would suggest. I'll play around a bit and see if I can come up with a better solution myself. However, my guess is that no matter what, there will always be a cast involved around the getInstance() method.
Re: Better singleton aspect [message #1042579 is a reply to message #1041806] Tue, 16 April 2013 15:53 Go to previous messageGo to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
Probably, there is no better solution. Otherwise, somebody might have found it already. However, using the word "find" for such a trivial case speaks volumes *sigh* 10 years ago I really expected that aspect-oriented programming could revolutionize programming (or clean the mess, at least). I tried using AOP in about 5 or 6 new Projects now. Each time I run into some trouble in the beginning :/ Either there are bugs in some products, other techical issues (modern platforms etc.) or I cannot solve trivial problems which should be in the scope of AOP like the posted issue. Colleagues of mine gave up a long time ago :/
Re: Better singleton aspect [message #1042648 is a reply to message #1042579] Tue, 16 April 2013 17:53 Go to previous message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
The best thing to do is to post to the AJ users mailing list, see here: http://eclipse.org/aspectj/userlists.php. And you are correct that not all problems are solvable using AspectJ. However, there is a certain class of problems that it excels at (modularizing crosscutting concerns like security, transactions, logging, etc).
Previous Topic:Plugin class calls logging
Next Topic:classes & methods nale
Goto Forum:
  


Current Time: Tue Apr 23 15:19:06 GMT 2024

Powered by FUDForum. Page generated in 0.02512 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top