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 the difference between architecture and design?

0 Answers  


What is the design pattern?

0 Answers  


when performing a functional test on a phone calculator,if pressing on a button does not function what do i do next

3 Answers   IBM,


What is a lazy initialization in singleton?

0 Answers  


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

1 Answers   HP,






Is oop a design pattern?

0 Answers  


Explain Inline Style Sheet?

1 Answers  


What is a behavioral design pattern?

0 Answers  


5.Develop an entity relationships diagram that identi&#64257;es physical entity relationships.

0 Answers  


Which Design Patterns you know?

4 Answers   Honeywell,


Show us an example of a design that can solve a business problem.

1 Answers  


What are the types of participants of the prototype design pattern you will get?

0 Answers  


Categories