1 package com.bytezreo.singleton;
2
3 /**
4 *
5 * @Description 单例模式 ---懒汉式
6 * @author Bytezero·zhenglei! Email:420498246@qq.com
7 * @version
8 * @date 上午9:50:29
9 * @
10 *
11 */
12
13 public class SingletonTest2 {
14 public static void main(String[] args) {
15 Order order1 = Order.getInstance();
16 Order order2 = Order.getInstance();
17
18 System.out.println(order1 == order2);
19 }
20 }
21
22 class Order
23 {
24 //1.私有化类的构造器
25 private Order()
26 {
27
28 }
29
30 //2.声明当前类的对象,没有初始化
31 //4.此对象也必须为 static的
32 private static Order instance = null;
33
34 //3.声明 public static的返回当前类对象的方法
35 public static Order getInstance()
36 {
37 if(instance == null)
38 {
39 instance = new Order();
40
41 }
42 return instance;
43 }
44
45
46 }