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 difference between GoF and J2EE patterns?

1 Answers   Accenture,


In which application lifecycle phases is an application architecture produced?

1 Answers   Microsoft,


Identify three types of systems or system upgrades that may be ideal candidates for a Waterfall Development Model strategy.

1 Answers  


Contact a system development program in your organization. Research how they analyzed their SYSTEM OF INTEREST (SOI), its OPERATING ENVIRONMENT, and their respective system elements. How was this analysis re&#64258;ected in the SOI architecture?

0 Answers   IAF,


How do you ensure quality of code ?

3 Answers   Infosys,






Dd you useuse OOA/OOD methodologies?did you use design patterns?

1 Answers   HP,


What is the publish/subscribe model?

1 Answers  


What is Architecture and what is design? Are they related?

3 Answers   IBM, Accenture,


What is impedance mismatch and How to solve the problem?

1 Answers   HP,


what is design patterns? which design patterns mostly used in .net? How it is used with examples?

1 Answers   IBM, Satyam,


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   Sampson, DBU, KSV College, University of Ibadan,


Explain Internal Style Sheets?

1 Answers  






Categories
  • Design Patterns Interview Questions Design Patterns (78)
  • UML Interview Questions UML (217)
  • OOAD Interview Questions OOAD (288)
  • Software Design Tools Interview Questions Software Design Tools (19)
  • Requirements Management Interview Questions Requirements Management (43)
  • Project Planning Interview Questions Project Planning (73)
  • Project Management Interview Questions Project Management (413)
  • Technical Writer Interview Questions Technical Writer (6)
  • DevOps Interview Questions DevOps (32)
  • MVC Model View Controller Interview Questions MVC Model View Controller (36)
  • Software Design AllOther Interview Questions Software Design AllOther (63)