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 |
Quartus II software version 6.0 is available for which operating systems (OS)?
What are the SDLC phases you have invloved ?
0 Answers Infosys, Tech Mahindra,
Explain what is good design?
What is the difference between factory and strategy design pattern?
What is the use of repository pattern?
What are External Style Sheets?
What is clean architecture?
what is data access layer?
What are the examples of the behavioral design patterns?
what is design patterns? which design patterns mostly used in .net? How it is used with examples?
What is Software development life cycle....??
How might prototyping be used as part of the SDLC?