• Advertisement
Sign in to follow this  

Unity Singletons!

This topic is 3619 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
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
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
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Innoc uous
      I'm working on a space game, and I suck at art. I would love to get some help from someone who is more skilled than me. Things I need include modular space ship parts and GUI elements. Nothing too fancy, just functional so I can get a prototype put together. This could potentially become a serious project, but for now this is just a hobby project.
       
      In this video, you can see a few things I already completed
      :2018-02-24 20-08-13.flv2018-02-24 20-08-13.flv
    • By Innoc uous
      If you want to incorporate noise into your shaders, the Turbulance Library has you covered. Using code I gathered from this library, I made a cginc file that contains all you need to easily implement noise into your unity shaders. Who knows how this stuff works, but man, does it work well!
       
      https://pastebin.com/LLCUpJut
       
      Here is an example of what you can create using these noise functions.
       
    • By Nio Martinez
      I'll be buying a new laptop as my workstation for building games, Mostly 3D but not hard core. 
       
      I'm stuck at choosing between these 2 specs below. Does this really matter and if so, can some one tell my how and why it matters. 
      Choice1:
      Intel core i5-8250U (8th gen Kabylake refresh)(6 MB Smart Cache, 1.6 GHz Base with Turbo Boost up to 3.4 GHz) 4 cores 8 threads
      RAM 8 GB DDR4 (2400 MHz)
      GPU 2 GB DDR5 Nvidia MX150 256 bit
      SSD: yes
      Choice2:
      Intel core i7-7500U 2.70GHz Base Processor (4M Cache, up to 3.50 GHz Boost) 2 Cores, 4 Threads
      RAM 4 GB DDR4 (1800 MHz)
      GPU 2 GB DDR5 Nvidia GeForce 940MX 256 bit
      SSD: No
       
    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By devbyskc
      Hi Everyone,
      Like most here, I'm a newbie but have been dabbling with game development for a few years. I am currently working full-time overseas and learning the craft in my spare time. It's been a long but highly rewarding adventure. Much of my time has been spent working through tutorials. In all of them, as well as my own attempts at development, I used the audio files supplied by the tutorial author, or obtained from one of the numerous sites online. I am working solo, and will be for a while, so I don't want to get too wrapped up with any one skill set. Regarding audio, the files I've found and used are good for what I was doing at the time. However I would now like to try my hand at customizing the audio more. My game engine of choice is Unity and it has an audio mixer built in that I have experimented with following their tutorials. I have obtained a great book called Game Audio Development with Unity 5.x that I am working through. Half way through the book it introduces using FMOD to supplement the Unity Audio Mixer. Later in the book, the author introduces Reaper (a very popular DAW) as an external program to compose and mix music to be integrated with Unity. I did some research on DAWs and quickly became overwhelmed. Much of what I found was geared toward professional sound engineers and sound designers. I am in no way trying or even thinking about getting to that level. All I want to be able to do is take a music file, and tweak it some to get the sound I want for my game. I've played with Audacity as well, but it didn't seem to fit the bill. So that is why I am looking at a better quality DAW. Since being solo, I am also under a budget contraint. So of all the DAW software out there, I am considering Reaper or Presonus Studio One due to their pricing. My question is, is investing the time to learn about using a DAW to tweak a sound file worth it? Are there any solo developers currently using a DAW as part of their overall workflow? If so, which one? I've also come across Fabric which is a Unity plug-in that enhances the built-in audio mixer. Would that be a better alternative?
      I know this is long, and maybe I haven't communicated well in trying to be brief. But any advice from the gurus/vets would be greatly appreciated. I've leaned so much and had a lot of fun in the process. BTW, I am also a senior citizen (I cut my programming teeth back using punch cards and Structured Basic when it first came out). If anyone needs more clarification of what I am trying to accomplish please let me know.  Thanks in advance for any assistance/advice.
  • Advertisement