我正在探索单例设计模式,我开发了一个类......
public class SingletonObject {
private static SingletonObject ref;
private SingletonObject () { //private constructor
}
public static synchronized SingletonObject getSingletonObject() {
if (ref == null)
ref = new SingletonObject();
return ref;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException ();
}
}
但是同步非常昂贵,所以我转向热切创建实例的新设计,而不是懒惰创建的实例。
public class Singleton {
private static Singleton uniqueInstance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return uniqueInstance;
}
}
但是请告诉我第二种设计比以前的设计有什么优势..!!