-
Notifications
You must be signed in to change notification settings - Fork 1
/
SingletonDemo3.java
49 lines (40 loc) · 1.38 KB
/
SingletonDemo3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Basics;
// https://www.youtube.com/watch?v=ODTnZQXWV9w&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=325
/**
* Singleton : Lazy initialization
*
* <p>1) via some method, make sure THERE IS ONLY 1 CLASS INSTANCE in the software program 2) how to
* implement ? -> check below steps
*
* <p>3) private static Order2 instance = null; // *** MAIN DIFFERENCE WITHIN EAGER & LAZY
* initialization (lazy : doesn't create at this stage)
*/
public class SingletonDemo3 {
public static void main(String[] args) {
// run
Order2 o1 = Order2.getInstance();
Order2 o2 = Order2.getInstance();
System.out.println(o1 == o2);
}
}
class Order2 {
// step 2) declare current class, *** NO NEED TO INSTANTIATE
// step 4) make below class as static
private static Order2 instance =
null; // *** MAIN DIFFERENCE WITHIN EAGER & LAZY initialization (lazy : doesn't create at this
// step 1) private constructor
// constructor
private Order2() {}
// stage)
// step 3) offer a public, static method that can return current class
public static Order2 getInstance() {
// NOTE : since we ONLY want to have ONE class instance
// so use below logic :
// -> if instance is null => instantiate a new class
// -> if instance != null => return current instance directly
if (instance == null) {
instance = new Order2();
}
return instance;
}
}