What are singletons?

Started by
13 comments, last by Emmanuel Deloget 16 years, 9 months ago
Quote:Original post by Zahlman
... The funny thing is, I'd been doing "static classes" in Java for a few years before C# came out, and everyone seemed to think it was blasphemous (never mind that the standard library does it in at least one case, although it generally seems to prefer nasty singleton setups). :)


Java needed singletons for some solutions. Enums (pre 1.5) were one. Of course, proper implementation of such enum required some 200+ lines of code. For each one.
Advertisement
So is it safe to say that your should avoid the use of singletons whenever possible? I have read anywhere.
Quote:Original post by Ticker14
So is it safe to say that your should avoid the use of singletons whenever possible? I have read anywhere.

Yes, because if something doesn't need to be a singleton, it shouldn't be.
Quote:Original post by Spoonbender
Static functions are ok.
Singletons aren't, and using them "to start out" is a really bad idea, because one of the biggest problems with them is that they're really hard to refactor away once you've got them.

I'm not expert, but I'd argue a little in favor of singletons in some uses. Some things are a good use of a singleton and to just label them as bad seems a little misleading. They are a common trap, but by no means should be disregarded as a whole.
Quote:Original post by NickGravelyn
Quote:Original post by Spoonbender
Static functions are ok.
Singletons aren't, and using them "to start out" is a really bad idea, because one of the biggest problems with them is that they're really hard to refactor away once you've got them.

I'm not expert, but I'd argue a little in favor of singletons in some uses. Some things are a good use of a singleton and to just label them as bad seems a little misleading. They are a common trap, but by no means should be disregarded as a whole.

Problem is that if you don't label things as bad, people will continue to use them because they believe that their use is legitimate. This is why, on these boards, you'll see many people that say that singletons are bad (and indeed, they are - in most cases. Correct use of this pattern happens only once in a while, and by once in a while I mean "twice in a lifetime" [smile]).

We lack any kind of authority that could say loudly "Singleton considered harmful" in a classical paper. In fact, all we have is a famous book that doesn't explain how to use it correctly and a bunch of bright guys who decided to write papers about it (beginning with Robert C Martin and Andrei Alexandrescu). Alexandrescu even got it worse, because he presented a highly technical solution to a non-existant problem. That doesn't mean that his article is bad - it presents a particular use of his modern C++ design - but the subject is rather infortunate, as it means that the singleton pattern is now proemiently featured in TWO major books about software design.

I think that gamedev.net is the only place where I saw a real, deep analysis of the singleton features and problems. Not being backed up by the "bright guys" out there does not mean that we are wrong (we have our bright guys too), but that certainly doesn't help to convince youngsters that the singleton pattern is too dangerous and should only be used in very, very rare occasions. 99.9% of the time, there is a far better solution to their particular problem.

So the official stance here seems to be "don't use singleton, they are bad". This is to be compared to the "don't use goto, it's bad" stance that originate in the late 60's.

This topic is closed to new replies.

Advertisement