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 a lazy initialization in singleton?

0 Answers  


What is mvp design pattern?

0 Answers  


Which design pattern is mvc?

0 Answers  


What are structural design patterns?

0 Answers  


What are the differences between the design patterns and the framework?

0 Answers  






Are you using singleton in your code?

0 Answers  


What are the categories in which the design patterns can be divided?

0 Answers  


What are the additional productivity features and enhancements included with Quartus II software version 6.0?

0 Answers  


How to analyze the design patterns ?

1 Answers   HP, Infosys, RRB,


What non-visual coding tools are available for web design?

0 Answers   Wipro,


What are the design patterns and How can they make life easier for software development ?

1 Answers   ABC, HP,


What is the creational design pattern?

0 Answers  


Categories