Jump to content
  • Advertisement
Sign in to follow this  
ToohrVyk

Unity Singletons!

This topic is 3884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
A link can be post to the wiki but not the content as it is copyrighted "(c) 2008 Victor Nicollet", no?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!