Sign in to follow this  
ToohrVyk

Unity Singletons!

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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!)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I've turned away from using Singletons because, if you need to make a class a Singleton just so you won't make more than one instance, you obviously don't have any control over ur need to make more than one instance of said class which would not allow ur program to work properly.

Its like constraining someone to only have one mouse at once because they may be stupid enuf to hook another one up at the same time and try to command windows with one mouse for each hand.

Share this post


Link to post
Share on other sites
Quote:
Original post by thre3dee
I've turned away from using Singletons because, if you need to make a class a Singleton just so you won't make more than one instance, you obviously don't have any control over ur need to make more than one instance of said class which would not allow ur program to work properly.


That would generally be a misuse of singletons, so you are right in avoiding it.

Singletons should be used to represent the external constraint that the class can only have one instance. In short, this constraint comes from the outside world, and not from the singleton itself. To reuse your mouse analogy, this is like preventing users from having two mice because there is a national law forbidding the use of more than one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this