懒汉模式
懒汉模式即生成对象实例的时机是在第一次需要该实例时,与饿汉模式相对应
1.基本懒汉式
public class Test_2 implements Serializable {
//懒汉在类加载时并不创建实例对象,而是置为null
private static Test_2 Instance=null;
//私有方法防止外部调用创建对象
private Test_2(){
System.out.println("构造方法");
}
//外部类获得单例对象方法,懒汉是在获取Instance时生成实例对象
public static Test_2 getInstance(){
if (Instance==null)
Instance=new Test_2();