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 |
Give me example of observer design pattern?
Is dependency injection a design pattern?
What is lexi design pattern?
Why have we used synchronized here?
What is design pattern ?
Did you use ooa/ood methodologies? Did you use design patterns?
Write the code for a singleton class?
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,
what is the difference between the Adapter Pattern and Proxy Patterns?its seems both are almost similar?
Explain what are 5 common problems in the software development process?
What are the examples of the behavioral design patterns?
What is the publish/subscribe model?