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

3. Discuss the concept of system transition strategy

0 Answers  


How to test the quality of design ?

1 Answers  


If there is a Model class,View class,Controller class then How these are internally related? Which layer objects instantiated in which layer? How they communicates?

4 Answers   Honeywell,


What is ESD?

1 Answers  


How do you ensure quality of code ?

3 Answers   Infosys,






what is cascading order?

1 Answers  


what is a Design Pattern?

5 Answers   TCS,


What are the advantages of thin clients?

1 Answers  


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 in&#64258;uence their approach next time? d. How was the V & V strategy implemented?

0 Answers  


Explain Internal Style Sheets?

1 Answers  


What is the S/W model used in the project. What are the optimization techniques used. Give examples ?

2 Answers   Wipro, Infosys, Symphony,


What is 'good design'?

2 Answers  






Categories
  • Design Patterns Interview Questions Design Patterns (76)
  • UML Interview Questions UML (58)
  • OOAD Interview Questions OOAD (54)
  • 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 (35)
  • Technical Writer Interview Questions Technical Writer (6)
  • Software Design AllOther Interview Questions Software Design AllOther (57)