This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Does Singleton has many definition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Does Singleton has many definition?" Watch "Does Singleton has many definition?" New topic
Author

Does Singleton has many definition?

alex han
Ranch Hand

Joined: Dec 13, 2002
Posts: 46
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
alex han
Ranch Hand

Joined: Dec 13, 2002
Posts: 46
yes thank you very much
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does Singleton has many definition?
 
Similar Threads
Static reference for object
private & static
difference between class with static method and singleton pattern
what is different in singleton design pattern
Singleton in multithreads