What is singleton design pattern



What is singleton design pattern..

Answer / lokesh

Singleton design pattern is a a creational pattern to
dictate how and when objects get created and it's main
purpose is to ensure that a class has only one instance

Example:
#define NULL 0

class Singleton
{
private:
Singleton(Singleton&){}
Singleton& operator =(Singleton&);
int nValue;
static Singleton* pSingleton;
Singleton():nValue(10)
{
}
public:
static Singleton*Instance()
{

if(NULL == pSingleton)
{
pSingleton = new Singleton();
}
return pSingleton;
}
void setValue(int val)
{
nValue = val;
}
int getValue()
{
return nValue;
}
};
Singleton* Singleton::pSingleton = NULL;

int main(int argc, char* argv[])
{
Singleton *abc = NULL;
cout<<Singleton::Instance()->getValue()<<endl;
Singleton::Instance()->setValue(20);

Singleton *xyz = NULL;
cout<<xyz->Instance()->getValue()<<endl;
Singleton *sss = Singleton::Instance();
return 0;
}

Is This Answer Correct ?    16 Yes 5 No

Post New Answer

More Design Patterns Interview Questions

What is design pattern ?

4 Answers   Accenture,


. Sell us on the ROI of investing in UI UX Design.

1 Answers  


When to Use a Factory Pattern?

3 Answers  


How do you choose a design pattern?

0 Answers  


What is the factory pattern in the design pattern?

0 Answers  


What are useful tools for developing and testing color schemes for web sites?

0 Answers   Wipro,


Quartus II software version 6.0 is available for which operating systems (OS)?

0 Answers  


What are the disadvantages of singleton pattern?

0 Answers  


What are the biggest trends in UX Design these days?

3 Answers  


What is the publish/subscribe model?

1 Answers  


What is the methodology to follow in collaboration with other team members like researchers, product managers, and developers?

1 Answers  


Which design pattern is mostly used in net?

0 Answers  


Categories