单利模式
单利模式,饿汉式与赖汉式写法,私有构造器保证了类在其他地方不能被实例化只能通过公用方法实例化对象。而懒汉式需要保证对象线程安全,否则会出现有多个对象的情况。
/*
*
// 单利饿汉式
private static Single instance = new Single();
private Single (){}
public static Single getInstance(){
return instance;
}*/
/**
* 单利赖汉式
*/
private static Single instance = null;
private Single (){}
public static synchronized Single getInstance(){
if(null == instance){
instance = new Single();
}
return instance;
}