Singleton Design pattern troubles

Started by
14 comments, last by Googol PL3X 15 years, 7 months ago
Quote:Original post by Googol PL3X
Hey rip-off, thanks for the advise, I've changed pretty well most of it. Could you further elaborate 2 and 5 of your post? Thanks


#2 is just what I said, avoid leading underscores.

Not sure what you want to know about #5. seems pretty clear to me. return a reference instead of a pointer, and just call Instance() when you need to use the singleton.
Advertisement
Quote:Original post by Googol PL3X
I've not done design patterns and such before tonight

So why exactly do you think you should use a Singleton?
Quote:Original post by DevFred
Quote:Original post by Googol PL3X
I've not done design patterns and such before tonight

So why exactly do you think you should use a Singleton?
There is nothing wrong with creating multiple instances of this class - this alone automatically rules out the need of a singleton but not the need of a global.
Whether or not the rest of the system requires global access to an instance of it depends on how well the rest of the system has been designed - if it is all well thought out then global access will not be needed either.
There is a huge block of text somewhere that has a long winded explenation of why not to use singletons. But Id like to throw my short version at you.

OK, before my version, Id just like to add another point. If this code is actually being used so you can get an idea of how singletons work, by all means keep it up, it might not ever come in handy, but its good to know. Ok, on with the point.

Unless you are working on code for a device with very limited memory (anything more then a hundred or so is likely not limited) where having more then one instance of a class would cause problems. Unless you need that kind of efficiently, just make a regular class and only use it once, it makes things alot easier down the road.
Required reading on Singletons.
I was creating a small game to play around with some of the design patterns, and try and learn how to use them.

I have read the article provided by phantom and decided I will try to avoid them in future, although just a bit of practice isn't going to hurt. Rather than doing this, I will probably just create an instance of Engine at the bottom of the class definition in Engine.h and make it extern and just include it where need be.

This topic is closed to new replies.

Advertisement