Singletons!

Started by
10 comments, last by ToohrVyk 16 years ago
Yes, singletons, again. In response to recent activity around the Singleton design pattern, I have written a short article on my website: link. My opinion is that while excellent resources (such as Performant Singletons or Singletonitis) explain why using singletons is a generally bad idea, they often take the aggressive "singletons should never be used" position, or one that is exceedingly close to it, which I feel reduces their impact to preaching to the choir. As such, I though I could contribute by providing a quick checklist to determine whether the pattern should be used, as well as superior alternatives in the cases where it shouldn't. I feel that this would be of great help in convincing people to stop using singletons based on misinformed decisions which lead them to writing code they don't need. Comments, suggestions and corrections are of course welcome. Feel free to link to the article if you feel it should be mentioned in a discussion.
Advertisement
That's helpful, at least to the extent that I understood it. I've only used singletons twice before, but now that you've explained it, it appears global variables and functions in a namespace would be a better solution.
A very nice article, and it's nice to see someone coming at the singleton subject from a more balanced angle, rather than the usual knee jerk reaction it usually gets ;)

One suggestion I would have is to add concrete examples as to when a singleton was (and should have been) used, and when it shouldn't have been used. Ideally an example should be given for each section (including the Monstate part), but I know how difficult that can be.

The reason I'm suggesting this is that many people will read and 'more-or-less' understand the theory, but a thought out example that shows them exactly what you mean can make it much clearer.
I agree with SpreeTree - examples would be nice.

But for what you've got it does nicely. I've added it to the forum FAQ as an alternative perspective to the "singletons are evil" truth which is already present [wink]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Good, concise post - I like it! One problem with the first 2 points is that some languages require you to group in an object & class, whether you like it or not. Take for instance C# - there are no concepts of functions, only methods - so you have to use a class.

Other than that, good stuff :)
Oliver Charles (aka aCiD2) [ Cycles Blog ]
Yeah i need examples!

As an Example: I want to write a Logger Class. Singleton yes or no?
Quote:Original post by ApochPiQ
I agree with SpreeTree - examples would be nice.

But for what you've got it does nicely. I've added it to the forum FAQ as an alternative perspective to the "singletons are evil" truth which is already present [wink]

Psst! Add it to the wiki! Instructional content should go in the wiki, and then forum FAQs can just be guidelines for posting in the forum. I'll raise this on mods.

(That goes for you, too, ToohrVyk. Nice article!)
A link can be post to the wiki but not the content as it is copyrighted "(c) 2008 Victor Nicollet", no?
Fine, I'll try to get some examples up (I always have a lot of trouble thinking of cases where singletons can be reasonably used).

Quote:Original post by Cycles
One problem with the first 2 points is that some languages require you to group in an object & class, whether you like it or not.


Almost: Java and C# force you to group everything into classes, but they don't force you to create objects. You can use static functions in zero-instance classes to represent free standing functions (see, for instance, the math libraries of both languages).

Quote:Original post by CmpDev
A link can be post to the wiki but not the content as it is copyrighted "(c) 2008 Victor Nicollet", no?


True.

I have no issue with releasing the content to anyone who wishes to publish it, for free, under a BSD-like or MIT-like license (that is, keep the copyright notice around, so that I get recognition). The issue I have with a wiki (as opposed to an article hosted on my web site, or even a forum post to a certain extent) is that, by virtue of the public domain, there is no need to reference the author of the content.

This topic is closed to new replies.

Advertisement