Possible Duplicate:
Efficient way to implement singleton pattern in Java
Can someone help me understand 2 ways to implement design pattern? I know one way, which is by making constructor private. Thanks for any pointers as well.
Possible Duplicate:
Efficient way to implement singleton pattern in Java
Can someone help me understand 2 ways to implement design pattern? I know one way, which is by making constructor private. Thanks for any pointers as well.
The simplest implementation consists in instanciating the Singleton directly in a private field of your Singleton class:
public class Singleton {
private static final instance = new Singleton();
private Singleton() {}
public Singleton getInstance() { return instance; }
}
Others implementations consist in "lazy loading" of your singleton: the instance is created only when you first call the "getInstance" method.
In order to protect your code in multi-thread context, there are various methods (double-null-check, inner class, etc.).
You will find more precisions with a quick Google search.
a simple example of Singleton design pattern is as follows:
public class SingletonPattern
{
private static SingletonPattern instance;
private SingletonPattern()
{ }
public static synchronized SingletonPattern getInstance(){
if (instance == null)
{
instance = new SingletonPattern();
}
return instance;
}
public static void main(String arg[])
{
System.out.println("The output of two instance:");
SingletonPattern sp=new SingletonPattern();
System.out.println("First Instance: "+sp.getInstance());
sp=new SingletonPattern();
System.out.println("Second Instance:"+sp.getInstance());
}
}
EDIT
the other way to create Singleton Pattern is as follows:
public enum SingletonPattern
{
INSTANCE;
// other methods
}
enums have sole constructor and it can not be invoked by programmer, so you can not make any more instances you don’t have to worry about serialization problems as enums are serializable by default
One simple way is to create a class with only static members. This effectively implements the singleton pattern, by never allowing multiple instances of the class.
public class Singleton {
private static String m_string;
private static int m_int;
static {
// initialization code...
}
public static void foo() {
}
}
Of course, the class is not passable as an object to other methods, but this has no meaning for singletons, as they can be referred to directly from anywhere in the code (this, of course, is their biggest disadvantage, since it creates very untestable code).
Another version using the volatile keyword. I hate the volatile keyword. No sane people would bring this version to a professor because of the probable question: "What does volatile exactly do and how does it work??"
public class Singleton {
private volatile static Singleton singleton;
private Singleton() {}
public static Singleton getInstance() {
if(singleton == null) {
synchronized (Singleton.class) {
if(singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
NOTE: