Skip to content

Commit

Permalink
Merge branch 'depromeet:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dep1ayer authored Sep 3, 2023
2 parents 1f49c84 + 770a52d commit 529d797
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Item. 25 ํ†ฑ๋ ˆ๋ฒจ ํด๋ž˜์Šค๋Š” ํ•œ ํŒŒ์ผ์— ํ•˜๋‚˜๋งŒ ๋‹ด์œผ๋ผ

> ์†Œ์Šค ํŒŒ์ผ ํ•˜๋‚˜์—๋Š” ๋ฐ˜๋“œ์‹œ ํ†ฑ๋ ˆ๋ฒจ ํด๋ž˜์Šค(ํ˜น์€ ํ†ฑ๋ ˆ๋ฒจ ์ธํ„ฐํŽ˜์ด์Šค)๋ฅผ ํ•˜๋‚˜๋งŒ ๋‹ด์ž
> ์ด ๊ทœ์น™๋งŒ ๋”ฐ๋ฅธ๋‹ค๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•œ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ์ผ์€ ์‚ฌ๋ผ์ง„๋‹ค.
> ์†Œ์Šค ํŒŒ์ผ์„ ์–ด๋–ค ์ˆœ์„œ๋กœ ์ปดํŒŒ์ผํ•˜๋“  ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ์ด๋‚˜ ํ”„๋กœ๊ทธ๋žจ์˜ ๋™์ž‘์ด ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.
---

### ํ†ฑ๋ ˆ๋ฒจ ํด๋ž˜์Šค๋ž€?

- ์†Œ์Šค ํŒŒ์ผ์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ์— ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ๋งํ•œ๋‹ค.
- ์ค‘์ฒฉ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค๋ฅผ ๋งํ•œ๋‹ค.

<br/>

### ์˜ˆ์‹œ (๋”ฐ๋ผ ํ•˜์ง€ ๋ง ๊ฒƒ!)

```java
# Song.java
class Genre {
static final String NAME = "POP";
}

class Song {
static final String NAME = "SONG";
}
```

```java
# Genre.java
Genre.java
class Genre {
static final String NAME = "K-POP";
}

class Song {
static final String NAME = "SONG";
}
```

```java
# main.java
# ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ
public class Test {
public static void main(String[] args) {
System.out.println(Genre.NAME + Song.NAME);
}
}
```

<br/>

### ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

- ์—ฌ๋Ÿฌ ํ†ฑ๋ ˆ๋ฒจ ํด๋ž˜์Šค๋ฅผ ํ•œ ํŒŒ์ผ์— ๋‹ด๊ณ  ์‹ถ๋‹ค๋ฉด ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

```java
public class AB {
private static class A {

}

private static class B {

}
}
```

<br/>

---

### Appendix

**์ปดํŒŒ์ผ๋Ÿฌ์˜ ๋™์ž‘:**

- ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ „์ฒด์ ์œผ๋กœ ๋ถ„์„ํ•˜์—ฌ ์ค‘๊ฐ„ ์ฝ”๋“œ(Intermediate code) ๋˜๋Š” ๋ชฉ์  ์ฝ”๋“œ(Object code)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
- ์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ๋ฒˆ์—ญํ•˜๋ฏ€๋กœ ์‹คํ–‰ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
- ๋ฒˆ์—ญ๋œ ๊ฒฐ๊ณผ๋ฌผ์€ CPU๊ฐ€ ์ง์ ‘ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๊ณ„์–ด๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

**์ธํ„ฐํ”„๋ฆฌํ„ฐ์˜ ๋™์ž‘:**

- ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„์”ฉ ์ฝ์–ด๋“ค์ด๊ณ , ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
- ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„์”ฉ ํ•ด์„ํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒˆ์—ญ๊ณผ ์‹คํ–‰์ด ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์ด๋ฃจ์–ด์ง„๋‹ค.
- ์‹คํ–‰ ์†๋„๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๋Š๋ฆฌ์ง€๋งŒ, ๊ฐœ๋ฐœ ๊ณผ์ •์—์„œ ์ˆ˜์ • ๋ฐ ๋””๋ฒ„๊น…์ด ์šฉ์ดํ•˜๋‹ค.

**์ž๋ฐ”์˜ ๋นŒ๋“œ ๊ณผ์ •:**

- ์ž๋ฐ”์˜ ๋นŒ๋“œ ๊ณผ์ •์€ ์ปดํŒŒ์ผ๊ณผ ์‹คํ–‰์˜ ๊ณผ์ •์ด ์„ž์—ฌ ์žˆ์Œ
- ์ปดํŒŒ์ผ ๋‹จ๊ณ„์—์„œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ค‘๊ฐ„ ํ˜•ํƒœ์ธ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์‹คํ–‰ ๋‹จ๊ณ„์—์„œ JVM์„ ํ†ตํ•ด ํ•ด๋‹น ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋กœ - ํ•ด์„ํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹
- ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ž๋ฐ” ์–ธ์–ด๋Š” ์šด์˜์ฒด์ œ์˜ ์ข…๋ฅ˜์™€ ์ƒ๊ด€์—†์ด ๋™์ผํ•œ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ”Œ๋žซํผ ํ˜ธํ™˜์„ฑ์„ ์ œ๊ณต
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Item 38. ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์—ด๊ฑฐ ํƒ€์ž…์ด ํ•„์š”ํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ผ

- ์—ด๊ฑฐํƒ€์ž… ์ž์ฒด๋Š” ํ™•์žฅํ•  ์ˆ˜ ์—†์ง€๋งŒ, `์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ธฐ๋ณธ ์—ด๊ฑฐ ํƒ€์ž…์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.`
- ์ด๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ๋Š” ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด ์ž์‹ ๋งŒ์˜ ์—ด๊ฑฐ ํƒ€์ž…(ํ˜น์€ ๋‹ค๋ฅธ ํƒ€์ž…)์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
- API๊ฐ€ (๊ธฐ๋ณธ ์—ด๊ฑฐ ํƒ€์ž…์„ ์ง์ ‘ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ ) ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด ๊ธฐ๋ณธ ์—ด๊ฑฐ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์“ฐ์ด๋Š” ๋ชจ๋“  ๊ณณ์„ ์ƒˆ๋กœ ํ™•์žฅํ•œ ์—ด๊ฑฐ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋Œ€์ฒดํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

---

### ํƒ€์ž… ์•ˆ์ „ ์—ด๊ฑฐ ํƒ€์ž…

```java
public final class Direction {

public static final Direction NORTH = new Direction("N");
public static final Direction SOUTH = new Direction("S");
public static final Direction EAST = new Direction("E");
public static final Direction WEST = new Direction("W");

private Direction() {
...
}
}
```

- jdk1.5 ์ด์ „์— enum์ด ์—†์„ ๋•Œ ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ์‹

<br/>

### ์—ด๊ฑฐ ํƒ€์ž…

```java
enum Direction {
NORTH, SOUTH, EAST, WEST;
}
```

- `์—ด๊ฑฐ ํƒ€์ž…์€ ๊ฑฐ์˜ ๋ชจ๋“  ์ƒํ™ฉ์—์„œ ํƒ€์ž… ์•ˆ์ „ ์—ด๊ฑฐ ํŒจํ„ด(typesafe enum pattern) ๋ณด๋‹ค ์šฐ์ˆ˜`ํ•˜๋‹ค.
- ๋‹จ, ์˜ˆ์™ธ๊ฐ€ ํ•˜๋‚˜ ์žˆ์œผ๋‹ˆ, ํƒ€์ž… ์•ˆ์ „ ์—ด๊ฑฐ ํŒจํ„ด์€ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ `์—ด๊ฑฐ ํƒ€์ž…์€ ํ™•์žฅํ•  ์ˆ˜ ์—†๋‹ค`.

- `์—ฐ์‚ฐ ์ฝ”๋“œ(operation code)`์—์„œ ์ด๋”ฐ๊ธˆ API๊ฐ€ ์ œ๊ณตํ•˜๋Š” `๊ธฐ๋ณธ ์—ฐ์‚ฐ ์™ธ ์‚ฌ์šฉ์ž ํ™•์žฅ ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ด์–ด์ค˜์•ผ ํ•  ๋•Œ`์™ธ์—๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์—์„œ ์—ด๊ฑฐ ํƒ€์ž…์„ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š์€ ์ƒ๊ฐ์ด๋‹ค.
- ์—ด๊ฑฐ ํƒ€์ž…์„ ํ™•์žฅํ•˜๋ฉด, ํ™•์žฅํ•œ ํƒ€์ž…์˜ ์›์†Œ๋Š” ๊ธฐ๋ฐ˜ ํƒ€์ž…์˜ ์›์†Œ๋กœ ์ทจ๊ธˆํ•˜์ง€๋งŒ ๊ทธ ๋ฐ˜๋Œ€๋Š” ์„ฑ๋ฆฝํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.
- ์—ด๊ฑฐ ํƒ€์ž…์„ ํ™•์žฅํ•˜๋ฉด ๊ธฐ๋ฐ˜ ํƒ€์ž…๊ณผ ํ™•์žฅ ํƒ€์ž…๋“ค์˜ ์›์†Œ ๋ชจ๋‘๋ฅผ ์ˆœํšŒํ•  ๋ฐฉ๋ฒ•๋„ ๋งˆ๋•…ํ•˜์ง€ ์•Š๋‹ค.
- ๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์—์„œ enum์„ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š๋‹ค.
- ์—ด๊ฑฐ ํƒ€์ž…์„ ํ™•์žฅํ•˜๋ ค๋ฉด `์—ด๊ฑฐ ํƒ€์ž…์ด ์ž„์˜์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ด์šฉ`ํ•˜๋ฉด ๋œ๋‹ค.
- `์—ฐ์‚ฐ ์ฝ”๋“œ์šฉ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜`ํ•˜๊ณ  `์—ด๊ฑฐ ํƒ€์ž…์ด ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„`ํ•˜๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ด ๋•Œ `์—ด๊ฑฐ ํƒ€์ž…์ด ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค์˜ ํ‘œ์ค€ ๊ตฌํ˜„์ฒด ์—ญํ• `์„ ํ•œ๋‹ค.

๋‹ค์Œ์€ Operation ํƒ€์ž…์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“  ์ฝ”๋“œ์ด๋‹ค.

**์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด ํ™•์žฅ ๊ฐ€๋Šฅ ์—ด๊ฑฐ ํƒ€์ž…์„ ํ‰๋‚ด ๋‚ธ ์ฝ”๋“œ**

```java
public interface Operation {
double apply(double x, double y);
}
```

```java
public enum BasicOperation implements Operation {
PLUS("+") {
public double apply(double x, double y) { return x + y; }
},
MINUS("-") {
public double apply(double x, double y) { return x - y; }
},
TIMES("*") {
public double apply(double x, double y) { return x * y; }
},
DIVIDE("/") {
public double apply(double x, double y) { return x / y; }
};

private final String symbol;

BasicOperation(String symbol) {
this.symbol = symbol;
}

@Override public String toString() {
return symbol;
}
}
```

- ์—ด๊ฑฐ ํƒ€์ž…์ธ BasicOperation์€ ํ™•์žฅํ•  ์ˆ˜ ์—†์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค์ธ Operation์€ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ณ , ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์—ฐ์‚ฐ์˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

- ์˜ˆ๋ฅผ ๋“ค์–ด ์•ž์˜ ์—ฐ์‚ฐ ํƒ€์ž…์„ ํ™•์žฅํ•ด ์ง€์ˆ˜ ์—ฐ์‚ฐ(EXP)์™€ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ(REMAINDER)์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž. ์ด๋ฅผ ์œ„ํ•ด ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ผ์€ Operation ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ์—ด๊ฑฐ ํƒ€์ž…์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ ๋ฟ์ด๋‹ค.

**ํ™•์žฅ ๊ฐ€๋Šฅ ์—ด๊ฑฐ ํƒ€์ž…**

```java
public enum ExtendedOperation implements Operation {
EXP("^") {
public double apply(double x, double y) {
return Math.pow(x, y);
}
},
REMAINDER("%") {
public double apply(double x, double y) {
return x % y;
}
};
private final String symbol;
ExtendedOperation(String symbol) {
this.symbol = symbol;
}
@Override public String toString() {
return symbol;
}
}
```

- ์ƒˆ๋กœ ์ž‘์„ฑํ•œ ์—ฐ์‚ฐ์€ ๊ธฐ์กด ์—ฐ์‚ฐ์„ ์“ฐ๋˜ ๊ณณ์ด๋ฉด ์–ด๋””๋“  ์“ธ ์ˆ˜ ์žˆ๋‹ค.
- Operation ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ž‘์„ฑ๋˜์–ด ์žˆ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

๊ฐœ๋ณ„ ์ธ์Šคํ„ด์Šค ์ˆ˜์ค€์—์„œ ๋ฟ ์•„๋‹ˆ๋ผ ํƒ€์ž… ์ˆ˜์ค€์—์„œ๋„, ๊ธฐ๋ณธ ์—ด๊ฑฐ ํƒ€์ž… ๋Œ€์‹  ํ™•์žฅ๋œ ์—ด๊ฑฐ ํƒ€์ž…์„ ๋„˜๊ฒจ ํ™•์žฅ๋œ ์—ด๊ฑฐ ํƒ€์ž…์˜ ์›์†Œ ๋ชจ๋‘๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

### ์ฒซ๋ฒˆ์งธ ๋Œ€์•ˆ) ExtendedOperation์˜ ๋ชจ๋“  ์›์†Œ ํ…Œ์ŠคํŠธํ•˜๊ธฐ

```java
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
test(ExtendedOperation.class, x, y);
}
private static <T extends Enum<T> & Operation> void test(
Class<T> opEnumType, double x, double y) {
for (Operation op : opEnumType.getEnumConstants())
System.out.printf("%f %s %f = %f%n",
x, op, y, op.apply(x, y));
}
```

- main ๋ฉ”์„œ๋“œ๋Š” test ๋ฉ”์„œ๋“œ์— ExtendedOperation์˜ class ๋ฆฌํ„ฐ๋Ÿด์„ ๋„˜๊ฒจ ํ™•์žฅ๋œ ์—ฐ์‚ฐ๋“ค์ด ๋ฌด์—‡์ธ์ง€ ์•Œ๋ ค์ค€๋‹ค. ์—ฌ๊ธฐ์„œ class ๋ฆฌํ„ฐ๋Ÿด์€ ํ•œ์ •์  ํƒ€์ž… ํ† ํฐ ์—ญํ• ์„ ํ•œ๋‹ค.

- opEnumType ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์„ ์–ธ(`<T extends Enum<T> & Operation> Class <T>`)์€ ์†”์งํžˆ ๋ณต์žกํ•œ๋ฐ, `Class ๊ฐ์ฒด๊ฐ€ ์—ด๊ฑฐ ํƒ€์ž…์ธ ๋™์‹œ์— Oepration์˜ ํ•˜์œ„ ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค`๋Š” ๋œป์ด๋‹ค.

- ์—ด๊ฑฐ ํƒ€์ž…์ด์–ด์•ผ ์›์†Œ๋ฅผ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๊ณ , Operation์ด์–ด์•ผ ์›์†Œ๊ฐ€ ๋œปํ•˜๋Š” ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

### ๋‘๋ฒˆ์จฐ ๋Œ€์•ˆ) Class ๊ฐ์ฒด ๋Œ€์‹  ํ•œ์ •์  ์™€์ผ๋“œ์นด๋“œ ํƒ€์ž…์ธ Collection<? extends Operation>์„ ๋„˜๊ธฐ๋Š” ๋ฐฉ๋ฒ•

```java
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
test(Arrays.asList(ExtendedOperation.values()), x, y);
}
private static void test(Collection<? extends Operation> opSet,
double x, double y) {
for (Operation op : opSet)
System.out.printf("%f %s %f = %f%n",
x, op, y, op.apply(x, y));
}
```

- ๋‘๋ฒˆ์งธ ์ฝ”๋“œ๋Š” ๊ทธ๋‚˜๋งˆ ๋œ ๋ณต์žกํ•˜๊ณ  ์—ฌ๋Ÿฌ ๊ตฌํ˜„ ํƒ€์ž…์˜ ์—ฐ์‚ฐ์„ ์กฐํ•ฉํ•ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
- ๋ฐ˜๋ฉด, ํŠน์ • ์—ฐ์‚ฐ์—์„œ๋Š” EnumSet๊ณผ EnumMap์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค.

๋‘ ๋Œ€์•ˆ ํ”„๋กœ๊ทธ๋žจ ๋ชจ๋‘ ๋ช…๋ น์ค„ ์ธ์ˆ˜๋กœ 4์™€ 2๋ฅผ ๋„ฃ์–ด ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

```
4.000000 ^ 2.000000 = 16.000000
4.000000 % 2.000000 = 0.000000
```

---

### ์—ด๊ฑฐ ํƒ€์ž…์—์„œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด ํ™•์žฅ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์†Œํ•œ ๋ฌธ์ œ์ 

- ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์—ด๊ฑฐ ํƒ€์ž…์„ ํ‰๋‚ด ๋‚ด๋Š” ๋ฐฉ์‹์—๋Š” `์—ด๊ฑฐ ํƒ€์ž…๋ผ๋ฆฌ ๊ตฌํ˜„์„ ์ƒ์†ํ•  ์ˆ˜ ์—†๋‹ค`๋Š” ์‚ฌ์†Œํ•œ ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.

- ์•„๋ฌด ์ƒํƒœ์—๋„ ์˜์กดํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ๋””ํดํŠธ ๊ตฌํ˜„์„ ์ด์šฉํ•ด ์ธํ„ฐํŽ˜์ด์Šค์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋ฐ˜๋ฉด Operation ์˜ˆ๋Š” ์—ฐ์‚ฐ ๊ธฐํ˜ธ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ฐพ๋Š” ๋กœ์ง์ด BasicOperation๊ณผ ExtendedOepration ๋ชจ๋‘์— ๋“ค์–ด๊ฐ€์•ผ๋งŒ ํ•œ๋‹ค.

- ์ด ๊ฒฝ์šฐ์—๋Š” ์ค‘๋ณต๋Ÿ‰์ด ์ ์œผ๋‹ˆ ๋ฌธ์ œ๋˜์ง„ ์•Š์ง€๋งŒ, ๊ณต์œ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ๋งŽ๋‹ค๋ฉด ๊ทธ ๋ถ€๋ถ„์„ ๋ณ„๋„์˜ ๋„์šฐ๋ฏธ ํด๋ž˜์Šค๋‚˜ ์ •์  ๋„์šฐ๋ฏธ ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ ์ค‘๋ณต์„ ์—†์•จ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Item 50. ์ ์‹œ์— ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค๋ผ

### ๋ฐฉ์–ด์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๊ธฐ

- ์•„๋ฌด๋ฆฌ ์ž๋ฐ”๋ผ๊ณ  ํ•ด๋„ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ์˜ ์นจ๋ฒ”์„ ์•„๋ฌด๋Ÿฐ ๋…ธ๋ ฅ์—†์ด ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹˜
- ์•…์˜์ ์ธ ์˜๋„๋ฅผ ๊ฐ€์ง„ ์‚ฌ๋žŒ๋“ค์ด ์‹œ์Šคํ…œ์˜ ๋ณด์•ˆ์„ ๋šซ์œผ๋ ค๋Š” ์‹œ๋„๊ฐ€ ๋Š˜๊ณ  ์žˆ์Œ
- ํ‰๋ฒ”ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋„ ์ˆœ์ „ํžˆ ์‹ค์ˆ˜๋กœ ํด๋ž˜์Šค๋ฅผ ์˜ค์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

**ํด๋ผ์ด์–ธํŠธ๊ฐ€ \*๋ถˆ๋ณ€์‹์„ ๊นจ๋œจ๋ฆฌ๋ ค ํ˜ˆ์•ˆ์ด ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋ฐฉ์–ด์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•ด์•ผ ํ•จ**

\*`๋ถˆ๋ณ€์‹`์ด๋ž€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํŠน์ • ์กฐ๊ฑด์ด ํ•ญ์ƒ ์ฐธ์ด๋ผ๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜๋Š” ์„ฑ์งˆ์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์•ˆ์ •์„ฑ๊ณผ ์˜ˆ์ธก ๊ฐ€๋Šฅ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

- ์˜ˆ์‹œ 1: ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ํ•œ ๋ฒˆ ์ •ํ•ด์ง€๋ฉด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋„๋ก ํ•˜์—ฌ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ์ธ๋ฑ์Šค๋กœ์˜ ์ ‘๊ทผ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
- ์˜ˆ์‹œ 2: final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•จ์œผ๋กœ์จ, ๊ทธ ๊ฐ’์„ ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๋ฐฉ์–ด์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ด€์ ์—์„œ ๋ถˆ๋ณ€์‹์„ ์ง€ํ‚ค๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•˜๋‹ค.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ถˆ๋ณ€์‹์„ ๊นจ๋œจ๋ฆฌ๋ ค ํ•  ๊ฒฝ์šฐ, ์ด๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

<br/>

### ์˜ˆ์‹œ 1

```java
public final class Period {
private final Date start;
private final Date end;

public Period(Date strart, Date end) {
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(
start + "๊ฐ€ " + end + "๋ณด๋‹ค ๋Šฆ๋‹ค.");
this.start = start;
this.end = end;
}

public Date start() {
return start;
}

public Date end() {
return end;
}

}
```

```java
Date start = new Date();
Date end = new Date();
Period p = new Period(start, end);
end.setYear(78);
```

์ด๋ ‡๊ฒŒ ๋ฐ”๋กœ end์˜ ๋‚ด๋ถ€๋ฅผ ๋ถˆ๋ณ€์‹์„ ๊นจํŠธ๋ฆด ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

_ํ•ด๋‹น Date ์— ๋Œ€ํ•ด์„œ ์ง€๊ธˆ์˜ ์ž๋ฐ”๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?_

- Date ๋Œ€์‹  ๋ถˆ๋ณ€์ธ Instant๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
- Java8 ์ด์ƒ๋ถ€ํ„ฐ๋Š” LocalDateTime์ด๋‚˜ ZonedDateTime์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.
- Date๋Š” ๋‚ก์€ API์ด๋‹ˆ ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋”์ด์ƒ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
- ์ด๋Ÿฌํ•œ Date ๋ง๊ณ ๋„ ๋‹ค๋ฅธ ์ƒํ™ฉ์—์„œ๋„ ๋ถˆ๋ณ€์‹์€ ๊นจ์งˆ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ณธ๋‹ค.

<br/>

### ์˜ˆ์‹œ 2

```java
Date start = new Date();
Date end = new Date();
Period p = new Period(start, end);
p.end().setYear(78)
```

Period์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค ๊ณต๊ฒฉ์„ ์ด๋Ÿฐ์‹์œผ๋กœ๋„ ๊นจํŠธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ง‰๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜๋‹ค.

```java
public Date start() {
return new Date(start.getTime());
}
public Date end() {
return new Date(end.getTime());
}
```

๋ฐ”๋กœ ์œ„์™€ ๊ฐ™์ด ๊ฐ€๋ณ€ ํ•„๋“œ์˜ ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

- ์•„๋ฌด๋ฆฌ ์•…์˜์ ์ธ ํ˜น์€ ๋ถ€์ฃผ์˜ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ผ๋„ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ์ข…๋ฃŒ ์‹œ๊ฐ„๋ณด๋‹ค ๋‚˜์ค‘์ผ ์ˆ˜ ์—†๋‹ค๋Š” ๋ถˆ๋ณ€์‹์„ ์œ„๋ฐฐํ•  ๋ฐฉ๋ฒ•์ด ์—†๋‹ค.
- Period ์ž์‹  ๋ง๊ณ ๋Š” ๊ฐ€๋ณ€ ํ•„๋“œ์— ์ ‘๊ทผํ•  ๋ฐฉ๋ฒ•์ด ์—†๋‹ค.
- ๋ชจ๋“  ํ•„๋“œ๊ฐ€ ๊ฐ์ฒด ์•ˆ์— ์™„๋ฒฝํ•˜๊ฒŒ ์บก์Šํ™”๋œ๋‹ค.
- ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ƒ์„ฑ์ž์™€ ๋‹ฌ๋ฆฌ ์ ‘๊ทผ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— clone์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.
- Period๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Date ๊ฐ์ฒด๊ฐ€ java.util.Date ์ž„์ด ํ™•์‹คํ•˜๊ธฐ ๋•Œ๋ฌธ

๋‹จ, ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒ์„ฑ์ž๋‚˜ ์ •์  ํŒฉํ„ฐ๋ฆฌ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

<br/>

### ์ •๋ฆฌ

- ํด๋ž˜์Šค๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›๋Š” ํ˜น์€ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ๊ฐ€๋ณ€์ด๋ผ๋ฉด ๊ทธ ์š”์†Œ๋Š” ๋ฐ˜๋“œ์‹œ ๋ฐฉ์–ด์ ์œผ๋กœ ๋ณต์‚ฌํ•ด์•ผ ํ•จ

- ๋ณต์‚ฌ ๋น„์šฉ์ด ๋„ˆ๋ฌด ํฌ๊ฑฐ๋‚˜, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ทธ ์š”์†Œ๋ฅผ ์ž˜๋ชป ์ˆ˜์ •ํ•  ์ผ์ด ์—†์Œ์„ ์‹ ๋ขฐํ•œ๋‹ค๋ฉด ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋Œ€์‹  ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ–ˆ์„ ๋•Œ์˜ ์ฑ…์ž„์ด ํด๋ผ์ด์–ธํŠธ์— ์žˆ์Œ์„ ๋ฌธ์„œ์— ๋ช…์‹œํ•ด์•ผ ํ•จ

0 comments on commit 529d797

Please sign in to comment.