Jump to content
  • Advertisement
Sign in to follow this  
Firecore

What is a singleton?

This topic is 3767 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 guys. sorry if this is a really noob question, but what exactly is a singleton? and where does it fit in a game? i came accross the word in one post, googled it but didnt really get any good results.

Share this post


Link to post
Share on other sites
Advertisement
Its an object which encapsulates and manages the instantiation of _ONLY_ one instance of itself. That's usually done through a "GetGlobalInstance()" static method in the object. It checks to see if the global instance has been instantiated yet, if not, it news it and returns that from then on.

You'd use it in a game anywhere you need access to the same global information but dont feel like passing the reference to that information around. For instance, a output logger.

You could pass around a reference to the logger object to every object who needs to log. Or you can make a global instance. If you do that, a singleton is a handy way of making sure that it gets instantiated the first time it's used.

Share this post


Link to post
Share on other sites
Something you should never use, according to most people here on GameDev.Net.

Singleton is a way to make a class "global" throughout the project, and is generally frowned upon by the OO community because it's basically an old fashioned hack to make C++ more like a C with classes language.

Share this post


Link to post
Share on other sites
Quote:
Original post by bzroom
Its an object which encapsulates and manages the instantiation of _ONLY_ one instance of itself.


Quote:
Original post by dbzprogrammer
Singleton is a way to make a class "global" throughout the project


It is both of these. A singleton is a class with global scope of which you can only ever have a single instance. They are generally evil constructs.

Share this post


Link to post
Share on other sites
@The OP: There is a lot of information available out there on the singleton pattern. You might start by reading some of the many singleton-related threads in the forum archives here (you'll learn what they are, and - one hopes - why not to use them).
Quote:
Original post by bzroom
It checks to see if the global instance has been instantiated yet, if not, it news it and returns that from then on.
This is nitpicking, but it doesn't have to be 'newed'; a reference to a local static object can be returned instead (in C++, at least - maybe you had a different language in mind).
Quote:
You'd use it in a game anywhere you need access to the same global information but dont feel like passing the reference to that information around.
Actually, in this case you'd just use a global object. You'd use a singleton when it's preferable (or at least appropriate) for an object to be globally accessable, and when it would be an error for more than one instance to be in existence at any one time. (Incidentally, I have yet to see an example case that meets both of these criteria.)

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
... You'd use a singleton when it's preferable (or at least appropriate) for an object to be globally accessable, and when it would be an error for more than one instance to be in existence at any one time. (Incidentally, I have yet to see an example case that meets both of these criteria.)


How about a memory manager which is used in a overloaded global new? This is the only instance(the singleton instance you could say :)) where I use it an it fits the pattern.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmail
How about a memory manager which is used in a overloaded global new? This is the only instance(the singleton instance you could say :)) where I use it an it fits the pattern.

Whaaa? Why would it be impossible to have multiple memory managers? You'd be passing the manager into the new clause anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Firecore
so basically a singleton is a class which only allows you to make one instance of that class?

Well, it depends.

A singleton, as singletons are intended to be, is a class which would be impossible to instantiate more than once.

A singleton, as programmers who like to think they're programming in "pure OO" think of it, is a class which they only want to instantiate once.

Either way, there's a mechanism to return the same object over and over rather than creating another one, but this should be looked at as a limitation rather than a feature. If it's the primary reason for having a singleton, you should really just be using a regular global variable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by dmail
How about a memory manager which is used in a overloaded global new? This is the only instance(the singleton instance you could say :)) where I use it an it fits the pattern.

Whaaa? Why would it be impossible to have multiple memory managers? You'd be passing the manager into the new clause anyway.


I totally agree. Another example is ResourceManagers. You can have multiple of those. Or ObjectManagers...
One trick is not to use the word "manager" in class names. I prefer the word "pool". So I have a ResourcePool and an ObjectPool, perhaps even a MemoryPool.

There are exceptions to the rule in my opinion that have to do with the ease of use of your code/classes. One example is to have a global logging class instance. You could also pass the logger to the constructor of every class, but this can be rather tedious.
On the other hand if you are writing unit tests it may be desirable to be able to pass separate loggers to different class instances.

In a sense the Singleton is in my opinion a lazy programmer's construction. Programmers who are too lazy pass a parameter around resort to the singleton for easy access of functionality.
But it never is that black and white. Generally, try to find a good balance between flexibility and ease of use, and be aware that having a Singleton might hamper your flexibility/design in the future.

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!