package com.leo;
/**
* 懒汉式单例模式 好处:延迟对象的创建 坏处:线程不安全
*/
public class SingleTonTest2 {
public static void main(String[] args) {
Order order1 = Order.getInstance();
Order order2 = Order.getInstance();
System.out.println(order1 == order2);
}
}
class Order {
// 1.私有化构造器
private Order() {
}
// 2.声明当前对象,没有初始化,必须为static
private static Order instance = null;
// 3.声明public、static的返回当前类对象的方法
public static Order getInstance() {
if (instance == null) {
instance = new Order();
}
return instance;
}
}