Advertisement

Singletons

Started by August 13, 2003 04:31 PM
16 comments, last by the_recon 21 years, 6 months ago
What are the Pros and Cons of Singletons?
Js
pros: encapsulation (you encapsulate a bunch of otherwise global variables)

cons: interdependability between modules - you''ll have to choose between:

  • creating something like externs.h, which includes all necessary headers (this can lead to dependency problems)
  • adding the extern declaration of the singleton class to the respective headers which isn''t always feasible and would lead to the fact that you have to include many headers in a cpp file to have access to all of the classes. Failing to include all necessary headers can also lead to declaring the "singleton" locally.

    You should try to avoid singletons where ever possible. For instance, the ultimate solution would be wrapping everything in one global class, though that''s almost never a good idea. In an opengl application, keep only the most vital classes as singletons: level, camera (possibly a part of the level), renderer, window etc. Wrap as much as is reasonable, at the same time trying maintain simplicity and efficiency in your project.
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
I''d suggest using only one singleton (if you have to. I do use this myself, even though I''m not too fond of the idea)... A "kernel" for your app if you will. Then just have all your other objects and variables (like your level, window, renderer etc) as members of this kernel class. That way it''s all neat and tidy and you can access everything from anywhere.

i.e. Kernel::Get().GetLevel() etc

I''ve only just discovered singletons... and I''ve gone mad with ''em!

I have the following classed as singletons...
CWindow (Window stuff)
CScreen (Err, screen stuff (OGL setup etc)
CLog (Only on log file needed afterall!)
CTexMan (Only one texture manager needed!)
CConsole (Only one console window needed!
CMoreOfEm? (Maybe more,I can''t remember)


I''m kinda getting scared by what the people above have said, how come ya don;t use singletons more?
(I guess I''ll find out in a couple of months or so)

Is it that, the whole project gets kinda "mystified", I mean like, "I wonder if this instance is being created now, or then)

Hmmm, no problems so far, singletons are my saviour, but I still need to learn much.

Download some source: http://www.DavesProgramming.com
another pro: not having to pass references up the whazoo. Especially with screen objects that are being drawn to.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
I am using 2 kinds of singletons mostly (am cautious)

the PPTEXTUREMANAGER and another one for vertexbuffers.
They are generally just a list of stuff and functions and store their data in hash tables. (Oh! And they''re singletons too)


I just don''t unserstand why I should avoid them. I mean, some people say that I should use templates as much as I can while others thinks they''re up to no good at all. I''m ery confused about what to use and what not to use since everyone have different opinions about this matter.
Js
Advertisement
I only heard about singletons the other day, when reading an article somewhere (maybe Gamedev) on making a Texture Manager. Can somebody point me towards a decent tutorial on them?

Thx

Lukerd.

Hyperdev

"To err is human, to really mess up requires a computer"
"To err is human, to really mess up requires a computer"
Head over to www.gametutorials.com the have an article that explains all you need to know about them.

Crisp: What kind if singletons are you using!?!? I''ve never experienced those sorts of problems your having and I use to overuse singletons like nothing. Maybe you should check out the page on gametutorials ti?



GDSAUBY GameDev Society For UnBanning YodaTheCoda
If you want to see yoda unbanned then put this in your sig
www.gamestutorials.com only gives you the source code. It doesn''t explain how things work. It only shows you how to make them work and it certainly never said when kt''s best to use singletons =/
Js
quote:
Original post by RamboBones
Crisp: What kind if singletons are you using!?!? I''ve never experienced those sorts of problems your having and I use to overuse singletons like nothing. Maybe you should check out the page on gametutorials ti?



What do you mean by "what kind of singletons"? I just said that used to have a lot of interdependence because of singletons. And I know what singletons are and how they''re used, thank you very much. What AP suggested is feasible, but not to my taste - a program is a whole, further encapsulating everything in it into one class is not really to the way I''d do things.

the_recon: I can''t understand what the problem is? Do you have trouble understanding what singletons are or are you just cofused about where and how to use them and what kind of effects their use has on your source code?

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

This topic is closed to new replies.

Advertisement