Sunday, May 18, 2008

Singleton

  • Singleton is a creational design pattern.
  • A design pattern to provide one and only instance of an object.
  • Make the constructors of the class private.
  • Store the object created privately.
  • Provide access to get the instance through a public method.
  • Can be extended to create a pool of objects.

Sample Program

#include <iostream>

using namespace std;

// Singleton class
class MySingleton {

    public:
        static MySingleton* iInstance;

    public:
        static MySingleton* GetInstance();

    private:
        // private constructor
        MySingleton();

};

MySingleton* MySingleton::iInstance = NULL;

MySingleton::MySingleton()
{
    cout << "In construtor ..." << endl;
}

MySingleton* MySingleton::GetInstance()
{
    if ( iInstance == NULL ) {
    iInstance = new MySingleton();
    }

    return iInstance;
}

void main()
{
    MySingleton* obj;
    obj = MySingleton::GetInstance();
}

OUTPUT:
In construtor ... (displayed only once)

13 comments :

  1. Thanks Mahesh for your contribution....Venkat

    ReplyDelete
  2. Also include the copy constuctor in private else other object can be created by copy construction

    ReplyDelete
  3. your singleton class with prohibit inheritance.

    ReplyDelete
  4. I don't think, copy constr will be a problem. If obj is created then we can use copy or copy constructor, But in singleton only one obj will be created and restricts for more object creation.all the refs. will refer to the same object. So no q. arises for assignment optr and copy constr. to become private.

    ReplyDelete
  5. Thanks helped a lot!!

    ReplyDelete
  6. Hey Friend. Thanks. Thank you so much.!!!!

    ReplyDelete
  7. #include

    using namespace std;

    // Singleton class
    class MySingleton {

    public:
    static MySingleton* iInstance;

    public:
    static MySingleton* GetInstance();

    private:
    // private constructor
    MySingleton();

    };

    MySingleton* MySingleton::iInstance = NULL;

    MySingleton::MySingleton()
    {
    cout << "In construtor ..." << endl;
    }

    MySingleton* MySingleton::GetInstance()
    {
    cout<<"HAI"<<endl;
    if ( iInstance == NULL ) {
    iInstance = new MySingleton();
    }

    return iInstance;
    }

    int main()
    {
    MySingleton *obj,*obj1;
    obj = MySingleton::GetInstance();
    obj1 = MySingleton::GetInstance();
    }

    ReplyDelete
  8. i expected output
    HAI
    In construtor ...
    but i got
    HAI
    In construtor ...
    HAI

    ReplyDelete
    Replies
    1. Nopes only one obj is being created here

      Obj1

      HAI - Call to getInstance()
      In constructor - creating first obj, MySingleton() call

      HAI - Call to getInstance()

      No call to constructor hence you have only one instance


      Delete
    2. Nopes only one obj is being created here

      Obj1

      HAI - Call to getInstance()
      In constructor - creating first obj, MySingleton() call

      HAI - Call to getInstance()

      No call to constructor hence you have only one instance


      Delete
  9. it means second object also creating

    ReplyDelete
  10. who will delete the instance object.
    I think It will lead to memory leak.

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top