/**
* 单例设计模式:
* 单例:一个类在内存中只有一个对象,别人不能再去创建对象
* 1.必须在该类中创建一个对象出来
* 2.私有化自己的构造器,防止外界通过构造器来创建新的对象
* 3.给外界提供一个方法,能够获取已经创建好的对象
*
*/
class ToolUtil{
//1.必须在该类中创建一个对象出来
private static ToolUtil instance = new ToolUtil();
//2.私有化自己的构造器,防止外界通过构造器来创建新的对象
private ToolUtil(){}
//3.给外界提供一个方法,能够获取已经创建好的对象
static public ToolUtil getInstance(){
System.out.println(“我是私有的”);
return instance;
}
}
public class SingleDesign {
public static void main(String[] args) {
ToolUtil tool1 = ToolUtil.getInstance();
System.out.println(tool1);
//ToolUtil instance1 = new ToolUtil();
}
}