Is this a good programming practice

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is this a good programming practice

Post by avansc »

Singletons are not inherently globally available/changeable(at least no more than any other local variable)
SINGLETONS is just a desing pattern, you could store it globally, but that does not mean singletons are bad, just the programmer.

a physics engine in a game is a singleton design, you cant change the physics from the NPC class (unless you THE PROGRAMMER provide that functionality).
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: Is this a good programming practice

Post by bugmenot »

The general argument against Singletons is that there isn't any real reason to have restriction the 'can only create one instance' by design because in theory there are very few scenarios where it must be enforced by design. For example, you may have more then one physics world instance if you have an odd multi-player mode. If you used a legacy framework or library which had the physics world class as a singleton, it would mean that you have to to some refractoring of that class any any others that relied on this fact whereas it could have originally been simply a local instance of the class without the design restriction of a singleton.

Offtopic: I be weary of using malloc to allocate memory for a class as it doesn't call the class's constructor whereas new does.
Post Reply