单例模式Singleton

单例模式Singleton

public class EagerSingleton {

	private static final EagerSingleton instance = new EagerSingleton();

	private EagerSingleton() {

	}

	public static EagerSingleton getInstance() {
		return instance;
	}

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

}

猜你喜欢

转载自injavawetrust.iteye.com/blog/2309779