Skip to main content
gamedev.net
🔒 Locked

Singletons

Started by Martel 0 replies 550+ views
Original Post
Martel
Martel
Seems like a lot of people hate singletons. I think that "pattern" in particular is too easy to abuse, like many things, but that it definitely has a place in software engineering. Especially in game development. You can''t decouple everything. Even if I agree to the idea that a singleton is sometimes just a glorified global variable, what''s a better way to model that coupling? To provide a more concrete question, say I''m designing a portable game engine that will work on PC, PS2, and Xbox. There will be several subsystems: input, graphics, sound, storage, etc. There will be several different implementations of those subsystems, but the interfaces themselves should be accessible wherever they are needed in the simplest possible way. There''s singleton. Then there''s having pointers to interfaces in classes that need to use those interfaces. Then there''s passing a pointer to the interface as function parameters. Seems like the singleton is the simplest, most efficient, and easiest to maintain- what''s not to like? What am I missing?
Shannon Barber
Shannon Barber
The implementation details, in C/C++, are very hairy.

If a global does the job why make it more complicated?

Even if you use a singleton instead of a global, your code still cannot be reentrant nor modular, thus not re-usable nor optimal for multi-threading.

Using a singleton allows for polymorphic behavior and automatic lifetime management (if it's implemented correctly).

[edited by - Magmai Kai Holmlor on September 5, 2003 10:43:38 PM]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.