0
点赞
收藏
分享

微信扫一扫

Java 单例模式(Singleton)


/** 
 * @author BestUpon 
 * @date 2010-6-13上午11:34:27 
 * @ask 饿汉式单利模式 
 * @answer 
 */  
public class HungerSingleton {   
    /** 
     * 一开始就初始化好了实例 
     */  
    private static HungerSingleton instance = new HungerSingleton();  
  
    private HungerSingleton() {  
    }  
  
    public static HungerSingleton getInstance() {  
        return instance;  
    }  
  
}

 

/** 
 *  
 * @author BestUpon 
 * @date 2010-6-13上午11:41:22 
 * @ask 懒汉式单例模式 
 * @answer 
 */  
public class LazySingleton {  
    private static LazySingleton instance = null;  
  
    private LazySingleton() {  
    }  
  
    public static LazySingleton getInstance() {  
        if(instance == null){  
            instance = new LazySingleton();  
        }  
        return instance;  
    }  
}

   

    参考:http://www.iteye.com/topic/691223

举报

相关推荐

0 条评论