Singleton Design Pattern

It is one of the simplest and commonly used pattern. This pattern makes sure, there shall be one and only instance(Object)of a class.  Idea behind this can be well understood by taking a day to day example of restaurant where same menu card is given to all customers to choose food or single catalog is shared by a departmental store or same/single access card swipe point is used by all visitors that want to enter a conference room, Same newspaper is given to a customer on a given day.

In Software programming, one of the common use of singleton is to keep common configurations of application in an object(Singleton Object). In mobile(Android) environment only one instance of an application is opened irrespective of number of times user has accessed/opened a given application such as one instance of chrome, Dial-Pad etc.
 
Singleton should not be seen as to make sure one only one of something but should be seen as single point of access of global entities.
 
Where Singleton can be used?
Singleton Design can be a best idea when one need to change/add an item in menu card, it just need to add it at single place and change shall reflect to all. It shall help a programmer to reduce time and errors to change code and maintain software in given situation.
Implementation specification can change from one language of code to other but basic idea remains same. We all know that constructor is a function that is used to create an object of a class, and Singleton can be achieved by declaring constructor in private area of class and let the member function to call constructor, also using functionality of Static Keyword.

Static (Keyword) with member variable/function of a class make sure that there shall be one and only copy of a variable  that declared with static would exist not matter how many objects are created.


Points to Remember
-- Should create static variable of class, so that one and only copy of a variable would exist. Moreover static function access static variables only.
-- Keep Constructor in private so that only class can instantiate (create) object, outside the class constructor should not be seen.

-- Keep function that would create constructor as static so that can be accessed with class name as object is not created yet (at the very beginning).

Implementation
Let's see how it can be implemented in C++


#include<iostream>
using  namespace std;
class Singleton
{
     static Singleton* object;  //Static Variable; contain pointer to Singleton Class object once it is created


     Singleton() //Constructor in Private;
     {
     }
public:

    static Singleton*  getInstance()  //Static function so that it can be accessed by class name because
    {                                                 // object is not created yet and it is used to create object
               if(object == NULL)
                {
                        cout<<"\n Singleton Object is created first time";
                        object= new Singleton();
                        return object;
                }
                else
                {
                        cout<<"\n Singleton Object is already created";
                        return object;
                }
        }
};
Singleton* Singleton:: object=NULL;
int main()
{
        int index=0;
        while(index<4) //Object is created multiple times
        {
                Singleton* object=Singleton::getInstance();
                index++;
        }
        return 0;
}

Output
Singleton Object is created first time
 Singleton Object is already created
 Singleton Object is already created
 Singleton Object is already created

Benefits
   - Single view to every client.


Fake Singleton
 
Some uses a work-around that a class behaves like Singleton class, this is achieved by making all member functions and variables as Static, as we all know that once we make a variable or function as static then it is associated with class rather than an object. If every thing is static in a class then it shall behave like static because very object created shall get the same Class variables and function. But it is not the best of idea because we are going away from the concept of OOP that object shall only be created when it is required but here object is already created (Virtually) even before use, this generally falls under early initialization case. Such type of classes are called as Static Class not Singleton Class Because every thing is static.



class Singleton
{
        static int variable1, variable2;
public:
        static int function()
        {
            variable1=10;
             
        }
        static int function2()
        {
          
           Variable2=20;  
        }
};
  

PREV:: Software Desing Pattern
              Is Singleton thread safe? ::NEXT
 
Your Comments /Suggestions and Questions are always welcome,  shall clarify with best of knowledge. So feel free to put Questions.

No comments:

Post a Comment