Jump to content
  • Advertisement
Sign in to follow this  
beun

Using a singleton for an application class?

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

Hi, I'd like to know whether you think that using a singleton for an application class is a good idea or not. I have a class called Application which contains pointers to other classes like a logger class, a window class, a renderer class etc... All my classes and member functions need access to the logger class, some need access to the renderer or the window class. I assumed that it was a bad idea to pass a reference of the application class to the constructor of each class. So is it OK to use a singleton for my application class, or are there better methods? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Why have you determined that it's a bad idea to use globals for things like your logger, your renderer, etc.?

Share this post


Link to post
Share on other sites
I would said it is a fine idea, and in fact should be the only way you should make an application class. My team and I are doing so in our game engine. There may be better methods, but to my knowledge the singleton should work perfectly fine as we have used it for some time now. Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Yes, that's another method. So it's OK to use singletons?

That isn't what I said... nor is it what I asked. I'm sure you already considered the globals option, but you jumped right over it and went on to singletons. Why? Your reasoning there is important in recommending a course of action.

Share this post


Link to post
Share on other sites
Well, I thought that using globals was considered "evil", and that singletons have some more encapsulation.

Share this post


Link to post
Share on other sites
Quote:
Original post by beun
Well, I thought that using globals was considered "evil", and that singletons have some more encapsulation.


Globals are only bad if they are poorly implemented. Just as there are some instances where the goto statement is actually the best course to take.

Completely ruling out an entire method seems foolish when there are certain times when it is a good idea to use them.

Share this post


Link to post
Share on other sites
Quote:
Original post by beun
Well, I thought that using globals was considered "evil", and that singletons have some more encapsulation.


Well, no, singletons have no more effective encapsulation than globals. As for the "evil" designation, it really depends on who you ask. People who are trying for a pure OO design, with strict adherence at all times to every principle of object oriented development (whatever they are), feel that globals are evil, but you should not accept that as canon until you have learned why they feel globals are evil. People who adopt a mixed-paradigm development methodology with a lot of OO but not entirely OO (that is, most people) feel that globals are bad to overuse but useful and good in moderation.

What everyone but EVERYONE agrees on, though, is that there are a lot of programmers out there who love the great taste of globals but have heard from a friend that they're EEEVIL and therefore use singletons to pretend that they're not using globals (even though the way they use singletons is exactly the way they'd use globals), and that those programmers are evil. Hint: If you can replace a singleton with a global, that's a bad place to be using a singleton, regardless of your development methodology.

Share this post


Link to post
Share on other sites
OK, so it's fine to use globals? Personally I haven't had any bad experiences with them, (and I also prefer (small) goto's over exception handling when it isn't necessary,) so I'll use globals for this case. Thanks for your quick responses [grin]

edit: And normally I wouldn't completely ignore things which are declared "evil [Period]", but I can understand some of the arguments of the global-bashers.

Share this post


Link to post
Share on other sites
Quote:

Well, I thought that using globals was considered "evil", and that singletons have some more encapsulation.


Encapsulation of... what exactly?

You need to understand why globals are considered evil. Can you think of anything that makes a Singleton less "global" than a global instance?

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!