ways to implement the Singleton design pattern [duplicate]
Possible Duplicate:
Efficient way to implement singleton pa开发者_如何学Cttern 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.
You can also use Enumeration
public enum Foo {
INSTANCE;
}
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;
}
}
- Yes it's thread safe.
- Yes I check if singleton is null twice.
- Yes it's different from Benoit's because this version lazily instantiate the singleton. The singleton is created (and the block synchronized) only if an instance is actually needed and not when the class is loaded.
- Yes it's different from Charlie's because I synchronize only once ever. After the singleton is created I'll never synchronize again. Also, synchronizing a block instead of a method reduces the overhead.
NOTE:
- It might not work with older version of Java. If you're java version is old, upgrade it. If you cannot, go with Benoit's that is thread safe as well.
- If you're not using threads, do not synchronize because it might reduce performances by a 100 times and go with Charlie's after removing the synchronized keyword.
- Please no drama.
精彩评论