- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
He "invented" them. He called the pattern "N-tons", but you have to manually code each n.
Admin
Or RPG.
Admin
My spell-checker rejects the words "n-sized" and "N-tons".
Admin
Another WTF is initializing a local variable to null in Java when you do not need to... In this case there is no problem with it, but in many cases it could create the potential for a NullPointerException.
Let the compiler do its job! Don't poke it in the eye and blind it!
Plus it will waste one CPU cycle.
No this is not a troll.
Admin
BART only has one stop in Fremont, and it's at the end of the line.
Admin
I wonder if this Singleton has an implementation of Inversion of Control, and there is a method to inject the Spring context it so desperatly wants.
Something like
I think it expects the Spring context to be injected from the outside before calling getInstance()
When the instance is requested and the Spring context is aleady known, the instance is taken from Spring.
If there is no Spring context, a new instance is created by initalizing Spring as specified in the applicationContext.xml. In there, the bean would be fully described with all its dependency injections. This new Spring context then becomes the context where the bean will be taken from on subsequent calls.
The only issue remaining is the caller of getInstance get a different instance then the one Spring creates, but both instances share all the statics Spring can inject, if not a Singleton then it is like a siamese twin for sure. The first instance will only be used by the caller, neither Spring or the Singleton keep refereces to it, it can be garbage collected as soon as the caller does not need it anymore, then only the one instance controlled trough Spring remains.
The example can do the singleton behaviour, but by itself it expects Spring to be in control over it. Take away Spring and it's out of control. If control has to be done trough other means then Spring, just instantiate trough the public constructor, inject all dependencies and keep your own reference.
Admin
Admin
If the "experts" can't come up with and agree on a simple, canonical description of how a simple programming problem should be dealt with, then I think the paradigm is broken.
Admin
Coming soon....the Tripleton
Admin
The 'Doubleton' was (first?) featured in Steven Goodwin's book "Cross platform game programming". Damned if I can remember how/why it was useful, though!
Admin
Obviously to alternate between two platforms :)
Admin
Was alex looking at the same code i am? Nothing he says here makes any sense.
"The instance variable is not static". The instance variable does not store the singleton instance; it is used to temporarily store the return value. The guy could have changed his code to "return new PublicationService(CONTEXTFILES);" instead, but the way he has it follows the one-path-in one-path-out rule which has its merits.
"The constructor grabs an instance of the class out of spring." No, the constructor does not do this. The constructor initializes the spring context.
"The first time you call getInstance, if has to create 2 instances". This is also not true. If I call PublicationService.getInstance(), it either loads a bean from the spring application context, or it creates a new instance. It will never create 2 instances.
The stupidest thing I see about this code is that it treats loading a spring application context as "not an error", so the application, when running, has kind of a schrodinger's cat kind of design where the PublicationService is maybe a singleton, and maybe not. There is also a minor WTF that he initializes instance to null, when it is guaranteed to be initialized in the if/else block. So, it's not a perfect design, but.... it seems like everyone is completely misinterpreting the code here?? Am i missing something obvious?
Apologies if someone else already pointed all this out. I came late to the party and just kind of skimmed the 3 pages of comments.
Admin
So anyone who can operate a browser is an OOP "expert"? Come on, now.
Admin
Damn, I'm looking good!
Admin
And of course, you forgot about the class PublicationService being final when it has a protected constructor! Right, you never know. Maybe in a moment of weakness the compiler could actually allow someone to override it?
Admin
Some details are more important than intent !!!
Admin
Maybe that's how his parents spelled it?
Admin
The real Doubleton Pattern
http://www.codeproject.com/KB/architecture/designpattern_doubleton.aspx?fid=179369&df=90&mpp=25&noise=3&sort=Position&view=Quick