Using a singleton for an application class?

Started by
121 comments, last by Rydinare 15 years, 4 months ago
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
Advertisement
Why have you determined that it's a bad idea to use globals for things like your logger, your renderer, etc.?
Yes, that's another method. So it's OK to use singletons?
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.
Alabama Man!!!
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.
Well, I thought that using globals was considered "evil", and that singletons have some more encapsulation.
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.
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.
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.
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?

This topic is closed to new replies.

Advertisement