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

๐ŸŒˆ Chapter 15: ์„œ๋กœ ๋‹ค๋ฅธ ํ†ตํ™” ๋”ํ•˜๊ธฐ

๋“œ๋””์–ด ์ด ๋ชจ๋“  ์ž‘์—…์˜ ์‹œ์ดˆ์ธ $5 + 10CHF์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ค€๋น„๊ฐ€ ๋๋‹ค.

public void testMixedAddition() {
Expression fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(
fiveBucks.plus(tenFrancs), "USD");
assertEquals(Money.dollar(10), result);
}

์ด๊ฒŒ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ฝ”๋“œ๋‹ค. ๋ถˆํ–‰ํžˆ๋„ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฌด์ง€ ๋งŽ๋‹ค.

public void testMixedAddition() {
Money fiveBucks = Money.dollar(5);
Money tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(
fiveBucks.plus(tenFrancs), "USD");
assertEquals(Money.dollar(10), result);
}

ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํŒจํ•œ๋‹ค. 10USD ๋Œ€์‹  15USD๊ฐ€ ๋‚˜์™”๋‹ค. Sum.reduce()๊ฐ€ ์ธ์ž๋ฅผ ์ถ•์•ฝํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์•„ ๋ณด์ธ๋‹ค. ์‹ค์ œ๋กœ๋„ ๊ทธ๋ ‡๋‹ค.

// Sum
public Money reduce(Bank bank, String to) {
int amount = augend.amount + addend.amount;
return new Money(amount, to);
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ์ธ์ž๋ฅผ ๋ชจ๋‘ ์ถ•์•ฝํ•˜๋ฉด ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผํ•  ๊ฒƒ์ด๋‹ค.

// Sum
public Money reduce(Bank bank, String to) {
int amount = augend.reduce(bank, to).amount + addend.reduce(bank, to).amount;
return new Money(amount, to);
}

ํ†ต๊ณผํ•œ๋‹ค. ํŒŒ๊ธ‰ ํšจ๊ณผ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ์ž๋ฆฌ์—์„œ ์ž‘์—…ํ•ด ๋‚˜๊ฐ€๊ธฐ ์‹œ์ž‘ํ•ด์„œ ๊ทธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊นŒ์ง€ ๊ฑฐ์Šฌ๋Ÿฌ ์˜ฌ๋ผ์˜ค๋„๋ก ํ•˜๊ฒ ๋‹ค.

// Sum
Expression augend;
Expression addend;

Sum ์ƒ์„ฑ์ž์˜ ์ธ์ž ์—ญ์‹œ Expression์ผ ์ˆ˜ ์žˆ๋‹ค.

// Sum
Sum(Expression augend, Expression addend) {
this.augend = augend;
this.addend = addend;
}

๊ทธ๋Ÿผ Money์ชฝ์€ ์–ด๋–ค๊ฐ€?
plus()์˜ ์ธ์ž๊ฐ€ Expression์œผ๋กœ ์ทจ๊ธ‰๋  ์ˆ˜ ์žˆ๋‹ค.

// Money
Expression plus(Expression addend) {
return new Sum(this, addend);
}

times()์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋„ Expression์ผ ์ˆ˜ ์žˆ๋‹ค.

// Money
Expression times(int multiplier) {
return new Money(amount * multiplier, currency);
}

์ด ์ฝ”๋“œ๋Š” Expression์ด plus()์™€ times() ์˜คํผ๋ ˆ์ด์…˜์„ ํฌํ•จํ•ด์•ผ ํ•จ์„ ์ œ์•ˆํ•˜๊ณ  ์žˆ๋‹ค. ์ด๊ฒŒ Money์˜ ์ „๋ถ€๋‹ค. ์ด์ œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋‚˜์˜ค๋Š” plus()์˜ ์ธ์ž๋„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

public void testMixedAddition() {
Money fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(
fiveBucks.plus(tenFrancs), "USD");
assertEquals(Money.dollar(10), result);
}

fiveBucks๋ฅผ Expression์œผ๋กœ ๋ฐ”๊พธ๊ณ  ๋‚˜๋ฉด ๋ช‡ ๊ตฐ๋ฐ๋ฅผ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋‹คํ–‰์Šค๋Ÿฝ๊ฒŒ๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ• ์ผ ๋ชฉ๋ก์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฐ ๊ณ„์† ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ์„  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ ์นœ๋‹ค.

public void testMixedAddition() {
Expression fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(
fiveBucks.plus(tenFrancs), "USD");
assertEquals(Money.dollar(10), result);
}

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ ์ž–๊ฒŒ Expression์— plus()๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•˜๋‹ค๊ณ  ์•Œ๋ ค์ค€๋‹ค. ์ •์˜ํ•ด์ฃผ์ž.

// Expression
Expression plus(Expression addend);

์ด์ œ Money์™€ Sum์—๋„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. Money? ๊ทธ๋ž˜, ๊ณต์šฉ์œผ๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.

// Money
public Expression plus(Expression addend) {
return new Sum(this, addend);
}

Sum์˜ ๊ตฌํ˜„์„ ์Šคํ… ๊ตฌํ˜„์œผ๋กœ ๋ฐ”๊พธ๊ณ , ํ• ์ผ ๋ชฉ๋ก์— ์ ์–ด๋‘์ž.

// Sum
public Expression plus(Expression addend) {
return null;
}

์ด์ œ ํ”„๋กœ๊ทธ๋žจ์ด ์ปดํŒŒ์ผ๋˜๊ณ  ํ…Œ์ŠคํŠธ๋„ ๋ชจ๋‘ ํ†ต๊ณผํ•œ๋‹ค.