๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๐ŸŒˆ Chapter 9: ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ๋Š” ์‹œ๊ฐ„

ํ†ตํ™”๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋ณต์žกํ•œ ๊ฐ์ฒด๋“ค์„ ์›ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฐ์ฒด๋“ค์ด ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ๋งŒ๋“ค์–ด์ง€๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๊ฒฝ๋Ÿ‰ ํŒฉํ† ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋‹น๋ถ„๊ฐ„์€ ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค ๋Œ€์‹  ๋ฌธ์ž์—ด์„ ์“ฐ์ž.

public void testCurrency() {
assertEquals("USD", Money.dollar(1).currency());
assertEquals("CHF", Money.franc(1).currency());
}

์šฐ์„ , Money์— currency() ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜์ž.

abstract String currency();

๊ทธ ๋‹ค์Œ ๋‘ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜์ž.

// Franc
String currency() {
return "CHF";
}

// Dollar
String currency() {
return "USD";
}

์šฐ๋ฆฐ ๋‘ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ๋™์ผํ•œ ๊ตฌํ˜„์„ ์›ํ•œ๋‹ค. ํ†ตํ™”๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ , ๋ฉ”์„œ๋“œ์—์„œ๋Š” ๊ทธ๋ƒฅ ๊ทธ๊ฑธ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

// Franc
private String currency;
Franc(int amount) {
this.amount = amount;
currency = "CHF";
}
String currency() {
return currency;
}

Dollar๋„ ๋˜‘๊ฐ™์ด ๋ณ€๊ฒฝํ•˜์ž.

private String currency;
Dollar(int amount) {
this.amount = amount;
currency = "USD";
}
String currency() {
return currency;
}

์ด์ œ ๋‘ currency()๊ฐ€ ๋™์ผํ•˜๋ฏ€๋กœ ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ currency() ๊ตฌํ˜„์„ ๋‘˜ ๋‹ค ์œ„๋กœ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๊ฒŒ ๋๋‹ค.

// Money
protected String currency;
String currency() {
return currency;
}

๋ฌธ์ž์—ด "USD"์™€ "CHF"๋ฅผ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋กœ ์˜ฎ๊ธด๋‹ค๋ฉด ๋‘ ์ƒ์„ฑ์ž๊ฐ€ ๋™์ผํ•ด์งˆ ๊ฒƒ์ด๊ณ , ๊ทธ๋ ‡๋‹ค๋ฉด ๊ณตํ†ต ๊ตฌํ˜„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
์šฐ์„  ์ƒ์„ฑ์ž์— ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜์ž.

// Franc
Franc(int amount, String currency) {
this.amount = amount;
this.currency = "CHF";
}

์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ ๋‘ ๊ณณ์ด ๊นจ์ง„๋‹ค.

// Money
static Money franc(int amount) {
return new Franc(amount, null);
}

// Franc
Money times(int multiplier) {
return Money.franc(amount * multiplier);
}

์ด์ œ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ "CHF"๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

// Money
static Money franc(int amount) {
return new Franc(amount, "CHF");
}

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ธ์ž๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

// Franc
Franc(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}

๋‚ด๊ฐ€ ์ด๋Ÿฐ ์ž‘์€ ๋‹จ๊ณ„๋ฅผ ๋ฐŸ์•„๊ฐ€๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ๊ธˆ ๋ฐฉ์–ด์ ์œผ๋กœ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋‚ด๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ์ •๋ง ์ด๋Ÿฐ ์‹์œผ๋กœ ์ผํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ฃผ์žฅํ•˜๋Š” ๊ฑด๊ฐ€? ์•„๋‹ˆ๋‹ค. ๋‚˜๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ด๋Ÿฐ ์‹์œผ๋กœ๋„ ์ผํ•  ์ˆ˜๋„ ์žˆ์–ด์•ผ ํ•œ๋‹ค๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

// Money
static Money dollar(int amount) {
return new Dollar(amount, "USD");
}

// Dollar
Dollar(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}
Money times(int multiplier) {
return Money.dollar(amount * multiplier);
}

์ง€๊ธˆ๊ณผ ๊ฐ™์€ ์ผ์„ TDD๋ฅผ ํ•˜๋Š” ๋™์•ˆ ๊ณ„์† ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ์ผ์ข…์˜ ์กฐ์œจ์ด๋‹ค. ์ข…์ข…๊ฑธ์Œ์œผ๋กœ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋‹ต๋‹ตํ•œ๊ฐ€? ๊ทธ๋Ÿฌ๋ฉด ๋ณดํญ์„ ์กฐ๊ธˆ ๋„“ํ˜€๋ผ. ์„ฑํผ์„ฑํผ ๊ฑท๋Š” ๊ฒƒ์ด ๋ถˆ์•ˆํ•œ๊ฐ€? ๊ทธ๋Ÿผ ๋ณดํญ์„ ์ค„์—ฌ๋ผ. TDD๋ž€ ์กฐ์ข…ํ•ด ๋‚˜๊ฐ€๋Š” ๊ณผ์ •์ด๋‹ค. ์ด์ชฝ์œผ๋กœ ์กฐ๊ธˆ, ์ €์ชฝ์œผ๋กœ ์กฐ๊ธˆ. ์ง€๊ธˆ๋„, ๊ทธ๋ฆฌ๊ณ  ์•ž์œผ๋กœ๋„ ์ •ํ•ด์ง„ ์˜ฌ๋ฐ”๋ฅธ ๋ณดํญ์ด๋ผ๋Š” ๊ฒƒ์€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋‘ ์ƒ์„ฑ์ž๊ฐ€ ์ด์ œ ๋™์ผํ•ด์กŒ๋‹ค. ๊ตฌํ˜„์„ ์ƒ์œ„ ํด๋ž˜์Šค์— ์˜ฌ๋ฆฌ์ž.

// Money
Money(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}

// Franc
Franc(int amount, String currency) {
super(amount, currency);
}

// Dollar
Dollar(int amount, String currency) {
super(amount, currency);
}