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 |
What is design pattern ?
. Sell us on the ROI of investing in UI UX Design.
When to Use a Factory Pattern?
How do you choose a design pattern?
What is the factory pattern in the design pattern?
What are useful tools for developing and testing color schemes for web sites?
Quartus II software version 6.0 is available for which operating systems (OS)?
What are the disadvantages of singleton pattern?
What are the biggest trends in UX Design these days?
What is the publish/subscribe model?
What is the methodology to follow in collaboration with other team members like researchers, product managers, and developers?
Which design pattern is mostly used in net?