Janeice wrote: 2: What, if any, are the problems with the Singleton pattern, exclusive of the issue of threadsafety (which is supposedly fixed by the "volatile" labeling)
All the other issues in the blog posts I already provided twice, and summarized above.
Regarding the double-locking/threading issue which appears to be a major focus for you:
If you'd read the IBM post you provided, you would have read this:
Editor's note: This article refers to the Java Memory Model before it was revised for Java 5.0; statements about memory ordering may no longer be correct. However, the double-checked locking idiom is still broken under the new memory model. For more information on the memory model in Java 5.0, see "Java theory and practice: Fixing the Java Memory Model" Part 1 and Part 2.
Part 1 of the reference includes the following:
It is worth noting that broken techniques like double-checked locking are still broken under the new memory model, and that "fixing" double-checked locking was not one of the goals of the new memory model effort. (However, the new semantics of volatile allow one of the commonly proposed alternatives to double-checked locking to work correctly, although the technique is still discouraged.)
Janeice DelVecchio wrote:I never said singletons were good or bad. I just don't know. Actually, my very first post said I made no assertions whether or not they should be used or not. I said I understood the basic concept of why someone would use it over a static variable. Rather, I think I just compared the two, not even made an assertion of why it should be used over a static variable.
What I said was that no one could tell me (on a foundational level) what the heck is wrong with using them. Everyone here has said "they're bad," "don't use them," et cetera.
I got links to blog posts from several years ago. Are these people respected in the field? I have no idea. No one said, "please see Joe Shmoe's blog, he's a such and such person in the blah blah industry for x years. He did studies for blah company."
When I questioned the sources, on an academic level, no one could defend the fact that the sources were blogs. No one could find a reputable source (or at least define what makes the blogs reputable).
If someone could tell me, in normal person language:
1: Why doesn't the JDK5 "volatile label fix" work to fix the singleton problem?
2: What, if any, are the problems with the Singleton pattern, exclusive of the issue of threadsafety (which is supposedly fixed by the "volatile" labeling)
I am really through with feeling like I have to defend myself. Just because I ask questions doesn't mean I'm questioning anyone's judgement, it just means I'm not going to take advice on face value no matter how long you've been following it.
Well, to cut all the crap why don't you just ask people to come up with a better solution to my postoffice Singleton?
What I'm using here is a variation of the Occam's razor, or the simplest solution is the correct one. Show a simpler solution to the postoffice Singleton and Singleton itself is in question. But until then, Singleton rules.
Joined: Nov 24, 2009
To my previous post.
You can find anything on the internet, including that OO sucks. The fact that some people on the internet think that Singleton sucks is in itself not a proof that Singleton is bad.
The fact that it's hard to perfectly implement a Singelton is in itself not a proof that Singleton sucks. Lots of very useful things are hard to implement, many of them even harder than a Singelton.
The fact that people misuses Singelton is not a proof that Singelton sucks. People misuses all sorts of language elements in every programming paradigm all the time.
So the only real test of the usefulness of Singleton is to apply Occam's razor as I explained in my previous reply.