-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication.java
More file actions
46 lines (38 loc) Β· 1.99 KB
/
Copy pathapplication.java
File metadata and controls
46 lines (38 loc) Β· 1.99 KB
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
package Chapter2.Day3;
import java.util.function.Supplier;
public class application {
public static void main(String[] args) {
Singleton singleton1 = Singleton.INSTANCE;
Singleton singleton2 = Singleton.INSTANCE;
System.out.println(singleton1 == singleton2);
/**
* μ μ ν©ν°λ¦¬ λ°©μ
* 1. ν΄λΌμ΄μΈνΈ μ½λ(API)λ₯Ό λ°κΎΈμ§ μκ³ μ±κΈν΄μ΄ μλκ² λ°κΏ μ μλ€.
* 2. μ μ ν©ν°λ¦¬λ₯Ό μ λ€λ¦ μ±κΈν΄ ν©ν°λ¦¬λ‘ λ§λ€ μ μλ€. (μμ΄ν
30μμ λ³Ό κ²μ΄λ€.)
* 3. μ μ ν©ν°λ¦¬μ λ©μλ μ°Έμ‘°λ₯Ό Supplier ν¨μν μΈν°νμ΄μ€λ₯Ό μ΄μ©νμ¬ μ¬μ©ν μ μλ€.
*
* 4. νμ§λ§ Java μ§λ ¬νμμ λͺ¨λ μΈμ€ν΄μ€ νλμ transient μ μΈ, κ·Έλ¦¬κ³ readResolve λ©μλλ₯Ό μ 곡νμ§ μμΌλ©΄
* μμ§λ ¬ν κ³Όμ μμ μλ‘μ΄ μΈμ€ν΄μ€κ° λ§λ€μ΄μ§λ€.
*/
Singleton2 singleton3 = Singleton2.getInstance();
Singleton2 singleton4 = Singleton2.getInstance();
System.out.println(singleton3 == singleton4);
// λ λ°©λ² λͺ¨λ 리νλ μ
μ μν΄ λ«λ¦΄ μ μλ€.
// κ°μ²΄λ₯Ό μμ±νλ κ³³μμ κ°―μλ₯Ό μΈμ΄ μμΈλ₯Ό λμ§ μ μλ€.
Supplier<Singleton2> sup = Singleton2::getInstance;
Singleton2 singleton5 = sup.get();
Singleton2 singleton6 = sup.get();
System.out.println("λ©μλ λ νΌλ°μ€ : " + (singleton5 == singleton6));
/**
* Enumμ μ΄μ©ν μΈμ€ν΄μ€ μμ±μ 리νλ μ
μλ μμ νκ³
* λ©ν° μ€λ λ νκ²½μμλ μμ νκ³
* μμ§λ ¬ν κ³Όμ μμλ μμ νλ€.
*
* Enum μ μμμ΄ λΆκ°λ₯νλ―λ‘ μμμ μ¬μ©ν μ μλ λ¨μ μ΄ μλ€.
* Interface ꡬνμ κ°λ₯νλ€.
*/
EnumSingleton enumSingleton = EnumSingleton.INSTANCE;
int sum = enumSingleton.add(1, 3);
System.out.println(sum);
}
}