一种思想
单例设计模式:核心思想:在内存中始终只有当前类的一个实例
饿汉式:
类一加载的时候就创建当前类对象----jdk提供的Runtime类:每一个Java应用程序都有自己的运行环境
class Runtime{
private static Runtime courrentRuntime=new Runtime();
private Runtime(){}
public static Runtime getRuntime(){
return courrentRuntime;
}
}
懒汉式:不会再类加载时候立即创建对象,在具体需要使用的时候才创建对象
存在懒加载/延迟加载(业务场景)----线程安全问题
class Teacher{
private Teacher teacher=null;
private Teacher(){}
public synchronized static Teacher getTeacher(){
if(teacher==null){
teacher=new Teacher();
}
return teacher;
}
}



