package com.design.patterns;
/**
* 懒汉式加载 线程不安全
* 1.将类构造器私有化,防止new对象
* 2.创建静态常量
* 3.提供公共获取对象的方法,只不过需要判断类对象是否为空,如果对象为空则进行创建对象,否则直接返回
*/
public class LazyLoadDem0_NotThreadSafe {
public static void main(String[] args) {
LazyNotSafeSingleton lazyNotSafeSingleton = LazyNotSafeSingleton.getInstance();
LazyNotSafeSingleton lazyNotSafeSingleton2 = LazyNotSafeSingleton.getInstance();
// 比较两个对象是否相等
System.out.println(lazyNotSafeSingleton == lazyNotSafeSingleton2);
// 查看两个对象的Hash值是否相同
System.out.println(lazyNotSafeSingleton);
System.out.println(lazyNotSafeSingleton2);
}
}
class LazyNotSafeSingleton {
private LazyNotSafeSingleton() {}
private static LazyNotSafeSingleton INSTANCE = null;
public static LazyNotSafeSingleton getInstance() {
if (INSTANCE == null) {
INSTANCE = new LazyNotSafeSingleton();
}
return INSTANCE;
}
}