Skip to content

Commit e161e16

Browse files
authored
Merge pull request #273 from Wave1994-Hoon/main
[#4][2๊ธฐ] ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ๋ง‰์œผ๋ ค๊ฑฐ๋“  private ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ผ
2 parents ca38bc0 + 423bee2 commit e161e16

1 file changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# item 4. ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ๋ง‰์œผ๋ ค๊ฑฐ๋“  private ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ผ
2+
## Why ??
3+
- ์ •์  ๋ฉค๋ฒ„๋งŒ ๋‹ด์€ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋ ค๊ณ  ์„ค๊ณ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.
4+
- ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
5+
6+
## ์ฃผ์˜ ์‚ฌํ•ญ
7+
- ์ถ”์ƒ ํด๋ ˆ์Šค๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์œผ๋กœ๋Š” ์ธ์Šคํ„ด์Šคํ™”๋ฅผ ๋ง‰์„ ์ˆ˜ ์—†๋‹ค.
8+
- ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋ฉด ๊ทธ๋งŒ์ด๋‹ค.
9+
- ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋“ค์ด ์ฝ”๋“œ๋ฅผ ๋ณด์•˜์„ ๋•Œ ์ƒ์†ํ•ด์„œ ์“ฐ๋ผ๋Š” ๋œป์œผ๋กœ ์˜คํ•ดํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
10+
11+
## How ??
12+
- ๋ชจ๋“  ์ƒ์„ฑ์ž๋Š” ๋ช…์‹œ์ ์ด๋“  ๋ฌต์‹œ์ ์ด๋“  ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.
13+
- ๋”ฐ๋ผ์„œ private์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์ƒ์„ฑ์ž ์ƒ์„ฑ์„ ๋ง‰๊ณ , ๋˜ํ•œ ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์— ์ ‘๊ทผ ๋˜ํ•œ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.
14+
15+
## Example
16+
- Effective Java ์ฑ…์—์„œ๋Š” Math ์œ ํ‹ธํด๋ž˜์Šค๋ฅผ ์˜ˆ์‹œ๋กœ ์–ธ๊ธ‰ํ•˜์˜€๋‹ค.
17+
```java
18+
public final class Math {
19+
20+
private Math() { } // private constructor
21+
22+
public static final double E = 2.7182818284590452354;
23+
24+
public static final double PI = 3.14159265358979323846;
25+
26+
private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
27+
28+
private static final double RADIANS_TO_DEGREES = 57.29577951308232;
29+
30+
@HotSpotIntrinsicCandidate
31+
public static double sin(double a) {
32+
return StrictMath.sin(a); // default impl. delegates to StrictMath
33+
}
34+
35+
public static double asin(double a) {
36+
return StrictMath.asin(a); // default impl. delegates to StrictMath
37+
}
38+
}
39+
```
40+
- ์ถ”์ƒํด๋ž˜์Šค ์˜ˆ์ œ
41+
```java
42+
/* ์ถ”์ƒ ํด๋ž˜์Šค */
43+
public class AbstractPrivateConstructorTest {
44+
45+
}
46+
47+
/* ํ•˜์œ„ ํด๋ž˜์Šค */
48+
public class PrivateConstructorTest extends AbstractPrivateConstructorTest {
49+
public PrivateConstructorTest() { }
50+
}
51+
52+
/* ํ…Œ์ŠคํŠธ */
53+
@Test
54+
void ์ถ”์ƒํด๋ž˜์Šค_Private_์ƒ์„ฑ์ž_ํ…Œ์ŠคํŠธ() {
55+
PrivateConstructorTest privateConstructorTest = new PrivateConstructorTest();
56+
57+
assertThat(privateConstructorTest).isInstanceOf(AbstractPrivateConstructorTest.class);
58+
}
59+
60+
```
61+
62+
## ์ถ”๊ฐ€ ๊ณ ๋ฏผ: ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„๊นŒ ??
63+
- Java Reflection ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค.
64+
- ํ•˜์ง€๋งŒ ๋‹จ์ˆœ ํ…Œ์ŠคํŠธ ์ปค๋ฒ„๋ฆฌ์ง€๋ฅผ ๋†’ํžˆ๊ธฐ ์œ„ํ•œ ํ…Œ์ŠคํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜๋ฏธ๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.
65+
- ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ: https://atin.tistory.com/630
66+
#### ์˜ˆ์ œ ํด๋ž˜์Šค
67+
```java
68+
public class PrivateConstructorTest {
69+
70+
private PrivateConstructorTest() {
71+
// throw new IllegalStateException();
72+
}
73+
}
74+
```
75+
76+
#### ์˜ˆ์ œ ํ…Œ์ŠคํŠธ
77+
````java
78+
class PrivateConstructorTestTest {
79+
80+
@Test
81+
void Private_์ƒ์„ฑ์ž_ํด๋ž˜์Šค_์ธ์Šคํ„ด์Šค_์ƒ์„ฑ_Test() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
82+
// given
83+
Constructor<PrivateConstructorTest> constructor = PrivateConstructorTest.class
84+
.getDeclaredConstructor();
85+
86+
// when
87+
constructor.setAccessible(true);
88+
PrivateConstructorTest privateConstructorTest = constructor.newInstance();
89+
90+
// then
91+
assertThat(privateConstructorTest).isInstanceOf(PrivateConstructorTest.class);
92+
}
93+
}
94+
````

0 commit comments

Comments
ย (0)