views:

1338

answers:

9

Do not waste your time with this question. Follow up to: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons


Please feel free to bitch on Singleton.

Inappropriate usage of Singleton may cause lot of paint. What kind of problem do you experienced with singleton? What is common misuse of this pattern?


After some digging into Corey's answer I discovered some greate articles on this topic.

+1  A: 

There's nothing inherently wrong with the Singleton pattern. It is a tool and sometimes it should be used.

brian
The Singleton pattern is evil and must be destroyed. There is never any good reason to use it. Put it this way, the Singleton pattern is basically just a fancy way of dressing up a global variable. Are globals good or bad?
1800 INFORMATION
Depends on the use, for a service to track instances of objects across all threads and other object instances... well global variables are pretty handy for that. If not we never would of got these great static objects.
Matthew Whited
+4  A: 

See this thread

Corey
A: 

I think a more appropriate question might be: In what situations is the use of a SIngleton Pattern inappropriate? Or what have you seen that uses a Singleton that shouldn't.

Mitchel Sellers
A: 

Most of the singleton patterns that I see written aren't written in a thread safe manner. If written correctly, they can be useful.

MagicKat
A: 

Basically singleton is a way to have static data and pretend it is not really static.

Of course I use it, but try to not abuse it.

jb
A: 

One basic problem with the original GoF design is the fact that the destructor isn't protected. Anyone with a reference to the singleton instance is free to destroy the singleton.

See John Vlissides update "To Kill A Singleton" in his book "Pattern Hatching" (Amazon link).

cheers,

Rob

Rob Wells
+3  A: 

Sometimes it can make your code more tightly coupled with the singleton class being refrerenced directly by name from different parts of your codebase. So, for example, when you need to test some part of your code and it references a singleton from a diferent part of the code you cannot easily fake that dependency with a mock object.

axk
A: 

There's nothing wrong with a singleton in itself, and as a pattern it fills a vital role in recognising the need for certain objects to only be created a single time.

What it is frequently used for is a euphemism for global variables as an attempt to get around global variable stigma, and it is this use that is inherently wrong. If a global variable happens to be the correct solution, using a singleton won't improve it. If it is (as is fairly common) incorrect to use a global variable, wrapping it in a singleton won't make it any more correct.

workmad3
+1  A: 

I haven't been exposed to the Singleton as much as some of the other posters have, but nearly all implementations that I have seen (in C#) could have been achieved with static classes/methods. I suppose you could argue that a static class is an implementation of the singleton pattern, but that's not what I've been seeing. I've been seeing people build up and manage these Singleton classes/objects when all they really needed was to use the static keyword.

So, I wouldn't say the Singleton pattern is bad. I'd say it's kinda like guns. I don't think guns are bad, but they can most certainly can be used inappropriately.

CrashCodes