|
| 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