What is singleton design pattern
Answer Posted / 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 View All Answers
What is the use of such a class?
Is singleton bean thread safe?
What is the behavioral design pattern?
What is viper architecture?
What is lexi design pattern?
What is singleton and factory pattern?
Write the code for a singleton class?
Contact a small, medium, and a large contract program within your organization. Interview the Technical Director or Project Engineer to identify the following information: a. Request the individual to graphically depict their development strategy? b. What factors drove them to choose the implementation strategy? c. What were some of the lessons learned from developing and implementing the strategy that would influence their approach next time? d. How was the V & V strategy implemented?
Can we have this pattern implemented using static class?
What are the types of participants of the prototype design pattern you will get?
Are you using singleton in your code?
What are the most important software design patterns?
What is the difference between adapter and facade?
Suppose we have file(ps), dont know how many records are there. Move half of the records to 2 files. How can we do?
What are useful tools for developing and testing color schemes for web sites?