๐ 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;
}
์ด์ ํ๋ก๊ทธ๋จ์ด ์ปดํ์ผ๋๊ณ ํ ์คํธ๋ ๋ชจ๋ ํต๊ณผํ๋ค.