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 are cascaded style sheets?
What is singleton design pattern
1 Answers CSS, Dimdim, Fulcrum Logic, TCS, Wipro,
What are the most important software design patterns?
What is the publish/subscribe model?
What is Software development life cycle....??
Hi, I have 9backlogs in btech and i am 2008 passed out,am working with an MNC from past 3 years.now, am willing to do MS, will it be any problem for me?
What skills do you think are necessary for a UI UX Designer?
what are design patterns..?what design patterns used in .net.
Why is it important to use systems analysis and design methodologies when building a system? Why not just build the system in whatever way appears to be “quick and easy”? What value is provided by using an “engineering” approach?
1 Answers DBU, KSV College, Sampson, University of Ibadan,
Which design pattern is mvc?
Which design patterns have you used in your project ?
What is proxy in design pattern?