Archived

This topic is now archived and is closed to further replies.

hammerstein_02

Singletons..

Recommended Posts

What is exactly meant by a singleton? I have done a search on google, but the links that looked helpful took me to book sites. I keep seeing singletons referred to and would just like to know what exactly is meant by the word.. thank-you, hope this isn''t a stupid question..

Share this post


Link to post
Share on other sites
A singeton is a class that only one instance of can exist in memory at any time. They commonly have a static method for accessing the single instance (SingletonClass::getInstance()).

[edited by - wayfarerx on August 16, 2002 4:52:34 PM]

Share this post


Link to post
Share on other sites
The Singleton is what's called a design pattern, a solution to a problem domain that occurs often and has a general solution. More specifically, a Singleton is a class (in C++) which has only one instance in memory at a time. Here's some code:

(**header file**)

#ifndef MY_SINGLETON
#define MY_SINGLETON

// TODO: Insert necessary headers here.

class CSingleton
{
public:
static CSingleton* Get()
{
if(!Instance)
Instance = new CSingleton;
return Instance;
};

// TODO: Insert additional public methods here.

protected:
CSingleton() {};

~CSingleton()
{
if(Instance)
delete Instance;
Instance = 0;
};

private:
static CSingleton *Instance;

// TODO: Insert additional private members here.
};

#endif

(**cpp file**)

#include "header.h"

static CSingleton *CSingleton::Instance = 0;

//TODO: Insert additional implementation here.


So, if you make your way through that code, you can see that the only possible way to use the class is by using the member "Get" (that's not a standard name, it's just what I use). Get returns a pointer to the class, from which you can call the other public members. Important things to note are that the constructor(s) is/are protected. This means that they can only be called by a static function, effectively. It also means that you can't simply instantiate a Singleton, nor can you use 'new' to allocate one, i.e.

CSingleton Foo; // error!
CSingleton *Bar = new CSingleton; // error!
CSingleton *Zork = CSingleton::Get(); // correct.


Ask if you need clarification. The code's not guaranteed to be flawless, either, but it seems to work.

Peace,
ZE.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on August 16, 2002 4:54:15 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Eckel writes:

"

The singleton

Possibly the simplest design pattern is the singleton, which is a way to provide one and only one instance of an object:


//: C11:SingletonPattern.cpp
//{L} ../TestSuite/Test
#include <iostream>
using namespace std;

class Singleton {
static Singleton s;
int i;
Singleton(int x) : i(x) { }
void operator=(Singleton&;
Singleton(const Singleton&;
public:
static Singleton& getHandle() {
return s;
}
int getValue() { return i; }
void setValue(int x) { i = x; }
};

Singleton Singleton::s(47);

int main() {
Singleton& s = Singleton::getHandle();
cout << s.getValue() << endl;
Singleton& s2 = Singleton::getHandle();
s2.setValue(9);
cout << s.getValue() << endl;
} ///:~

The key to creating a singleton is to prevent the client programmer from having any way to create an object except the ways you provide. To do this, you must declare all constructors as private, and you must create at least one constructor to prevent the compiler from synthesizing a default constructor for you.


At this point, you decide how you’re going to create your object. Here, it’s created statically, but you can also wait until the client programmer asks for one and create it on demand. In any case, the object should be stored privately. You provide access through public methods. Here, getHandle( ) produces a reference to the Singleton object. The rest of the interface (getValue( ) and setValue( )) is the regular class interface.


Note that you aren’t restricted to creating only one object. This technique easily supports the creation of a limited pool of objects. In that situation, however, you can be confronted with the problem of sharing objects in the pool. If this is an issue, you can create a solution involving a check-out and check-in of the shared objects.

"

Share this post


Link to post
Share on other sites
Right.. I getcha

Thank-you so much for the prompt replies, so if I am understanding the use of a singleton correctly, something like my graphics class would have need for being a singleton as I don''t want to create two instances of the class, or two instances of my input class etc..

I appreciate the help guys, thanks once again.

Share this post


Link to post
Share on other sites
quote:
Original post by jen6677
can you just make all fields and methods of a class static, would that work as a singleton?


Not quite. That''s the Monostate pattern. Singletons can
have virtual functions. Monostate classes can''t, for example.



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites