GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Java Patterns
Next Question 
 Java Patterns  |  Question 1 of 19    Print  
What is singleton pattern ??

  
Total Answers and Comments: 5 Last Update: March 10, 2008     Asked by: manepalli 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sampra
 
when you want to restrict the object creation of a particular call is more tahn one then use singiltion class

Above answer was rated as good by the following members:
yathirajulu, Gerda
October 24, 2006 18:58:46   #1  
Jay        

RE: What is singleton pattern ??
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.
 
Is this answer useful? Yes | No
December 04, 2006 05:11:33   #2  
hi_goura Member Since: December 2006   Contribution: 6    

RE: What is singleton pattern ??
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
 
Is this answer useful? Yes | No
April 05, 2007 09:18:45   #3  
Sony V George        

RE: What is singleton pattern ??

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


 
Is this answer useful? Yes | No
April 12, 2007 06:05:24   #4  
Divesh Kumar        

RE: What is singleton pattern ??
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
 
Is this answer useful? Yes | No
March 10, 2008 05:05:54   #5  
sampra Member Since: February 2008   Contribution: 279    

RE: What is singleton pattern ??
when you want to restrict the object creation of a particular call is more tahn one then use singiltion class
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape