What is singleton pattern ??

Editorial / Best Answer

hi_goura  

  • Member Since Dec-2006 | Dec 4th, 2006


Singleton pattern is a design pattern by which we cannot create more than one instance of the class. For a fresh request to get an instance, a new instance will be returned. And further requests will return you the referenceto the earlier one. There is no keyword singleton to make your class as "Singleton Class". You have to design your class accordingly. First of all make the default constructor private and don't give any other constructor to restrict creation of multiple instances from outside of your class. Have a public static method that returns you the reference to the same class (this is called Factory method). Have a class-level (static) private variable whose data-type is same class. Inside the static method, check if the static variable is null then create a new object, assign that to the static variable and return it. If that is not-null, that means this is not the first request, return the same static variable. Now If someone wants an instance of your class, (s)he must call YourClass.staticMethod(). No other option. Thanks-Gourahari

Showing Answers 1 - 13 of 13 Answers

Jay

  • Oct 24th, 2006
 

If a system only needs one instance of a class, and that instance needs to be accessible in many different parts of a system, you control both instantiation and access by making that class a singleton.

hi_goura

  • Dec 4th, 2006
 

Singleton pattern is a design pattern by which we cannot create more than one instance of the class. For a fresh request to get an instance, a new instance will be returned. And further requests will return you the referenceto the earlier one. There is no keyword singleton to make your class as "Singleton Class". You have to design your class accordingly. First of all make the default constructor private and don't give any other constructor to restrict creation of multiple instances from outside of your class. Have a public static method that returns you the reference to the same class (this is called Factory method). Have a class-level (static) private variable whose data-type is same class. Inside the static method, check if the static variable is null then create a new object, assign that to the static variable and return it. If that is not-null, that means this is not the first request, return the same static variable. Now If someone wants an instance of your class, (s)he must call YourClass.staticMethod(). No other option. Thanks-Gourahari

Sony V George

  • Apr 5th, 2007
 

goura, There is one more way to create an instance of the calss. Thatz by using clone() method, for avoiding that. we have to override the clone() method.

Tel me if i went wrong

Divesh Kumar

  • Apr 12th, 2007
 

You need to override clone method, in order to prevent the class from being clone only if the class implements Cloneable interface. Object class by itself does not implement Cloneable so a CloneNotSupportedException will be thrown if you call clone on an instance of class that does not implement Cloneable

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions