`
lhc1986
  • 浏览: 160409 次
  • 性别: Icon_minigender_1
  • 来自: 帝都
社区版块
存档分类
最新评论

普通单例、文艺单例、二逼单例

阅读更多
  • 普通单例
public class Singleton {
	private static Singleton uniqueInstance = new Singleton();
	
	private Singleton(){
		
	}
	
	public static Singleton getInstance(){
		return uniqueInstance;
	}
}
  • 文艺单例
public class Singleton {
	private static Singleton uniqueInstance;
	
	private Singleton(){
		
	}
	
	public static Singleton getInstance(){
		if(uniqueInstance == null){
			synchronized (Singleton.class) {
				if(uniqueInstance == null){
					uniqueInstance = new Singleton();
				}
			}
		}
		return uniqueInstance;
	}
}
  •  2逼单例
public class Singleton {
	private static Singleton uniqueInstance;

	private Singleton() {

	}

	public static synchronized Singleton getInstance() {
		if (uniqueInstance == null) {
			uniqueInstance = new Singleton();
		}
		return uniqueInstance;
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics