现在的位置: 首页 > 综合 > 正文

简单的单例模式

2012年10月17日 ⁄ 综合 ⁄ 共 377字 ⁄ 字号 评论关闭
package com.demo.sw.test;

public class HungerySingleton {
	
	private HungerySingleton(){
		
	}
	
	private static HungerySingleton s = new HungerySingleton();
	
	public static HungerySingleton getInstance(){
		return s;
	}

}


package com.demo.sw.test;

public class LazySingleton {
	private LazySingleton(){
		
	}
	private static LazySingleton ls = null;
	
	public static LazySingleton getInstance(){
		if(null == ls){
			ls = new LazySingleton();
		}
		return ls;
	}
	

}

抱歉!评论已关闭.