私はシングルトンの設計パターンを調査していましたが、クラスを開発しました...
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;
}
}
しかし、2番目のデザインが前のデザインよりも優れている点を教えてください..!!