   Singletons
  Over the last few years I have read and done textbook stuff (projects and studies) on C++. However, there still seems to be thing that I have no idea/little knowlage about; such as singletons.How are singletons bad (maybe cause they are global) and what are the perks of using them (I predict performance)?Also, what are these "God Classes" that I have heard about a few times around here? Are they similar to singletons? Pros/Cons of using them?Sorry, just curious on some things that I have rarely heard of or never heard of while learning from the textbooks. Hope I have not bothered anyone.   
  Singleton's are bad bad bad. They lock a class into allowing only one instance of it to ever being created. The other problem is it tends to introduce dependency's where there shouldn't be.They should be avoided at all costs, they are the definitive anti-pattern.   ;  Singletons and god classes have one big thing in common, they flag a broken and/or horrible design.  Singletons are mainly used for patching holes in broken designs instead of taking the take to refactor properly.  Meanwhile god classes completely break the rules of OOP and stick a million unrelated or loosely related things in one single class.   ;Quote:Original post by PromitI was originally going to write something myself. I decided it wasn't necessary; Washu, jpetrie, and others did most of the actual work already. This list will be updated whenever I feel like it. The ones I like are in bold.Washu's JournalWhy are you infected with Singletonitis?Singletonitis, Part 2Singletonitis, Part 3ThreadsSingletons and Memory ManagementWhen to use and not to use singletonsRate my engine!learning singletonsSharing data between game statesGood/Generic C++ ProgrammingSingleton managersSingleton versus Monostate Pattern The One: A Singleton Discussion (companion to article)A Singleton Texture Manager for OpenGL (companion to article)ArticlesA Singleton Texture Manager for OpenGLThe One: A Singleton DiscussionUsing ManagersThe Singleton Class ClusterExternal LinksSingleton Pattern (wikipedia)Singleton Pattern (c2 wiki)Singleton Considered StupidUse your singletons wiselyWhy Singletons are EvilSingletonitis;  Thank you for all of the reading. It really helped and I now know that I have never had either of the types and hopefully will not ever have one. I think I did come close to a god class once though. Gotta be careful.[Edited by - Shlaklava on September 10, 2007 8:05:23 PM];  Ok...just to ask another question.I have an application being designed and I have been wondering if there is a need for more than one graphic-type class (or a  class that simplifies OpenGL or DirectX to a few commands such as DrawModel(location)) and even a class that handles mouse and keyboard input? That would make them singletons but the idea makes sense as you only need one keyboard/mouse per computer and one screen to write on, but is it bad?   ;Quote:Original post by ShlaklavaOk...just to ask another question.I have an application being designed and I have been wondering if there is a need for more than one graphic-type class (or a  class that simplifies OpenGL or DirectX to a few commands such as DrawModel(location)) and even a class that handles mouse and keyboard input? That would make them singletons but the idea makes sense as you only need one keyboard/mouse per computer and one screen to write on, but is it bad?Who says you only need one "graphics" class?  What about multiple windows?With OpenGL w/GLX an application can drive multiple displays on multiple machines concurrently.  Who says there's just one display?Furthermore, who says you only "need" one keyboard/mouse/screen per computer?  Why can't I hang an extra keyboard, mouse and monitor off my system and make a 2-user desktop system?  (Well, Windows wouldn't like this, but this kind of display system architecture was common when graphics systems were very expensive; you can ammortize the processing power over several displays and make a more cost effective system.  Dunno if you could do it in linux, maybe you could.)   ;Quote:Original post by ShlaklavaOk...just to ask another question.I have an application being designed and I have been wondering if there is a need for more than one graphic-type class (or a  class that simplifies OpenGL or DirectX to a few commands such as DrawModel(location)) and even a class that handles mouse and keyboard input? That would make them singletons but the idea makes sense as you only need one keyboard/mouse per computer and one screen to write on, but is it bad?If you only need one instance of a certain class, then only create one instance of it. That alone isn't enough to justify a singleton. Remember: singletons can only ever have one instance, and are globally visible. Just because you need one of these doesn't mean you want both.   ;  Once again, thanks everyone. This has been very enlightening and will help me in the future.   ;  This is one of the ways in which the Singleton is such n over-used pattern.It is very often mis-used because it sounds like it would be a good pattern or a solution to all your problems to the inexperienced when, in fact, it will only introduce many, many more problems in the long run.Classically, this is the "but I only want/need one" argument. The Singleton is not a pattern which solves the case of "I only want/need one" it solves the explicit case of "Exactly one instance must exists always, and I must have global access" -- If you introduce a singleton for anything less than that exact scenario, then you are choosing to use what is not only an incorrect pattern, but you are choosing to use a pattern that will be a detriment to your program over the medium/long term.So, what *is* a justified use for a singleton then? Its difficult to come up with an example in application software simply because there is no (or nearly no) justification in that domain. Simply put, if you're writing an application or game, and you're tempted to use a Singleton, just resign yourself to accepting the fact that it would be a poor decision.The Singleton can be useful in other domains, such as Embedded or Control applications, where the hardware platform essentially necessitates the "Exactly one with global access" definition, but even then it is best avoided or hidden away behind other patterns if possible.The singleton should really be considered Arcane Knowlege, correctly wielded by only the most experienced C++ mages.   