Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

the_recon

Singletons

This topic is 5455 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

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • 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!