Archived

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

zacharya

singleton

Recommended Posts

A singleton is a class of which there will be only one instance.

The static keyword is used for various things, depending on kontext. In C I believe it is used to declare constant values. In Java it declares methods and variables that are common to all instances of a class.

Share this post


Link to post
Share on other sites
Oh, and I can only speak for Java here, but the connection between the two is, that if you declare a class with all methods and variables static, there will be only one "real" instance of the class. You can practically create more instances, but since they all refer to the same data and methods, they are just different references to the same object.

Share this post


Link to post
Share on other sites
Static in C++:
for global variables: can only be used within the compilation unit (file) that they appear in (instead of normal variables that can be used with the extern keyword from other units)
for inside-function-variables: are only initialized once, and keep their value between function calls
for class member variables/functions: are independant of instances of the class, can not access non-static member variables and functions of the class.

Singleton:
A mechanism for making sure that a class only has one instance, which can be accessed by a global method. Usually requires some use of the static keyword to implement.
An advantage of singletons over globals is that they are created whenever they are needed (the instance is gotten by calling a function), normal globals may in some instances be referenced before they are initialized, leading to hard-to-find problems.


// Example one:
class Singleton1{
public:
static Singleton1& get(){
static Singleton1 s;
return s;
}
};

// Example two [this one is not automatically deleted, and still has problems
// when the s variable is uninitialized when it gets referenced]:
class Singleton2{
public:
static Singleton2& get(){
if (s == NULL)
s = new Singleton2;
return *s;
}
private:
static Singleton2* s;
};

Singleton2* Singleton2::s = NULL;


(edit: made a line too long in the code)

[edited by - marijnh on October 23, 2003 6:47:12 AM]

Share this post


Link to post
Share on other sites
Like the others have said, a singleton is basicly a class that you can only have one instance of. That single instance is actually declared in the class itself as a private static member. To prevent other instances of your singleton class to be created, you make the constructors and the assignment operator as private. You usually write an accessor member function that returns the static instance, and when this member function is called for the first time the instance is created on the heap (ie: it is allocated via new).

Here is the simple singleton example that I have just explained:


// Singleton example


class Singleton
{
public:
static Singleton *GetInstance()
{
if (instance == NULL)
instance = new Singleton;
return instance;
}

static void ReleaseInstance()
{
if (instance != NULL)
{
delete instance;
instance = NULL;
}
}

private:
static Singleton *instance;

Singleton() {};
Singleton(const Singleton& rhs) {};
Singleton& operator=(const Singleton& rhs) {};
~Singleton() {};
};

Singleton* Singleton::instance = NULL;

int main()
{
Singleton *instance = Singleton::GetInstance();
Singleton::ReleaseInstance();

return 0;
}


[edited by - chacha on October 23, 2003 10:15:13 AM]

Share this post


Link to post
Share on other sites
However, I think that you should be careful with singletons. I tempts you think in a global-variable way that most often is inappropriate. A singleton is like a more elegant global variable, and as such I don''t think it is really needed in most places. I noticed this when programming an assignment, where a "master" singleton solved a lot of problems we had with the design.

This convenient solution did however create a lot of dependencies between classes that perhaps should have been avoided at the first place. Things can get really nasty really fast if the design gets too tangled, and I think singletons can be a quick solution that often can cause this.

Just a non-technical rant

Share this post


Link to post
Share on other sites