Thursday, November 1, 2007

Singleton design pattern

What is a singleton?

It means nothing much than its word meaning. In c++ point of view Singleton means a class that can only have one object. So,

A singleton class is a class that will have only one instance at any time.

When to go for singleton?

You must use a singleton pattern if your class must only have one instance. For example,

* If your class is doing some loging to a console you may need to log all info to same console. Then go for singleton. Allow only one instance of the class to be created.
* If your class uses some device which must only be created/initialized ones and multiple object should not access the same go for singleton.
* If from more than one place you want to use the same instance of a class we can definitely think about using singleton.

How to make a class singleton?

There are different ways for making a singleton class. But there are some common thing to do while making a Singleton class. They are,

* Make the constructor of the class private
* Make the copy constructor of the class private
* Overload the = operator as private
* Write a static member function which returns the static object created inside class/function.



Implementation

#include
using namespace std;

class SingleTon
{
static SingleTon m_OneAndOnlyObject;
int m_nValue;
private:
// Constructor is declared as private
SingleTon( int nVal ):m_nValue( nVal ){;}
// Copy constructor is declared as private
SingleTon( const SingleTon& );
// = operator is declared as private
void operator = ( SingleTon& );
public:
static SingleTon& getInstance()
{
// The same instance will be always returned
return m_OneAndOnlyObject;
}
void SetValue( int nVal ){ m_nValue = nVal; }
int GetValue( void ){ return m_nValue; }
};

// Initializing the static object
SingleTon SingleTon::m_OneAndOnlyObject( 10 );

int main(int argc, char* argv[])
{
// Getting a reference
SingleTon& Obj1 = SingleTon::getInstance();
// Printing the value using Obj1-
// and setting the value using Obj1
cout << "Obj1.m_nValue: " << Obj1.GetValue() << endl;
Obj1.SetValue( 20 );

// Making second reference and printing the value
SingleTon& Obj2 = SingleTon::getInstance();
cout << "Obj2.m_nValue: " << Obj2.GetValue() << endl;

// Changing the value of m_nValue using Obj2-
// and Printing using Obj1
Obj2.SetValue( 30 );
cout<<"Obj1.m_nValue: "< return 0;
}

In the above program we have created 2 objects. If we set the value of m_nValue using any of the reference it will reflect in all others because there is only one object.

Variants:

There are different methods for making a singleton class

1. By creating static object inside the getInstance function.

Code:

static SingleTon& getInstance()
{
// The same instance will be always returned
static SingleTon OneAndOnlyObject(10);
return OneAndOnlyObject;
}

2. Creating in heap

Code:
static SingleTon* m_OneAndOnlyObjectHeap; // Change the static object to a pointer

static SingleTon* getInstance()
{
// The same instance will be always returned
if( !m_OneAndOnlyObjectHeap )
{
m_OneAndOnlyObjectHeap = new SingleTon( 10 );
}
return m_OneAndOnlyObjectHeap;
}
SingleTon* SingleTon::m_OneAndOnlyObjectHeap( 0 );//Static member initialization


The variants differ in creation of object. Everything else is the same.