• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Does Singleton has many definition?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is Singleton's original definition?
in developerworks is:
public class Singleton {
private static Singleton s;
private Singleton(){};
/**
* Class method to access the singleton instance of the class.
*/
public static Singleton getInstance() {
if (s == null)
s = new Singleton();
return s;
}
}
and in other is:
public class Singleton {
  private Singleton(){}

  private static Singleton instance = new Singleton();
  public static Singleton getInstance() {
    return instance;   
   }
}
or:
public class Singleton {
  private static Singleton instance = null;
  public static synchronized Singleton getInstance() {
 
  if (instance==null)
    instance=new Singleton();
  return instance;   }
}
now i want to know how many definition about Singleton?which is most used?
thanks a lot
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The definition of Singleton is: a class ensuring that there is only one instance of it and providing a global point of access to it by a class method.
What you presented are just the two most typical implementations.
The one assigning the instance at the field declaration has the advantage that it is inherently thread-safe.
The other, using lazy initialization, needs some amount of explicite synchronization, but defers the creation of the instance until you really need it for the first time.
Did that help?
PS: Singleton is one of the simplest design patterns, but also one of the easiest to be misused. Be *very* wary at applying it!
See also http://c2.com/cgi/wiki?SingletonPattern
 
alex han
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes thank you very much
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
BTW, just in the case that "hit demo" isn't your real name - could you please adjust your display name so that it conforms to our naming policy? Thank you very much!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic