设计模式之Singleton

时间:2022-11-02 21:49:45
 class Singleton {

     private Singleton() {
} private static Singleton instance; // v0.1
// public static Singleton getInstance(){
// if(instance==null) instance=new Singleton();
// return instance;
// } // v0.2
// //Double-check singleton
// public static Singleton getInstance() {
// if(instance==null) {
// synchronized (Singleton.class) {
// if(instance==null) instance = new Singleton();
// }
// }
// return instance;
// } // v0.3
private static class SingletonHolder{
static final Singleton INSTANCE=new Singleton();
} public static Singleton getInstance(){
return SingletonHolder.INSTANCE;
} }