π Chapter 12: λλμ΄, λνκΈ°
public void testSimpleAddition() {
Money sum = Money.dollar(5).plus(Money.dollar(5));
assertEquals(Money.dollar(10), sum);
}
κ·Έλ₯ Money.dollar(10)
λ₯Ό λ°ννλ μμΌλ‘ κ°μ§ ꡬνμ ν μλ μλ€. νμ§λ§ μ΄λ»κ² ꡬνν΄μΌ ν μ§ λͺ
ννλ―λ‘ λ€μκ³Ό κ°μ΄ νκ² λ€.
// Money
Money plus(Money addend) {
return new Money(amount + addend.amount, currency);
}
μ΄ κ°λ¨ν μμ μμ Bank
κ° ν μΌμ μ λ§ νλλ μλ€. μΌλ¨ κ°μ²΄λ§ νλ μλ€λ©΄ μ€μΌμ΄λ€.
public void testSimpleAddition() {
// ...
Bank bank = new Bank();
Money reduced = bank.reduce(sum, "USD");
assertEquals(Money.dollar(10), reduced);
}
λ Money
μ ν©μ Expression
μ΄μ΄μΌ νλ€.
public void testSimpleAddition() {
// ...
Expression sum = five.plus(five);
Bank bank = new Bank();
Money reduced = bank.reduce(sum, "USD");
assertEquals(Money.dollar(10), reduced);
}
$5
λ₯Ό λ§λλ건 κ°λ¨νλ€.
public void testSimpleAddition() {
Money five = Money.dollar(5);
Expression sum = five.plus(five);
Bank bank = new Bank();
Money reduced = bank.reduce(sum, "USD");
assertEquals(Money.dollar(10), reduced);
}
μ΄κ±Έ μ»΄νμΌνλ©΄ μ΄λ»κ² ν΄μΌ νλ? Expression
μΈν°νμ΄μ€κ° νμνλ€.
// Expression
interface Expression
Money.plus()
λ Expression
μ λ°νν΄μΌ νλ€.
//Β Money
Expression plus(Money addend) {
return new Money(amount + addend.amount, currency);
}
μ΄κ±΄ Money
κ° Expression
μ ꡬνν΄μΌ νλ€λ λ»μ΄λ€.
// Money
class Money implements Expression
μ΄μ λΉ Bank
ν΄λμ€κ° νμνλ€.
// Bank
class Bank
κ·Έλ¦¬κ³ Bank
μλ reduce()
μ μ€ν
μ΄ μμ΄μΌ νλ€.
// Bank
Money reduce(Expression source, String to) {
return null;
}
μ΄μ μ»΄νμΌμ΄ λκ³ , λ°λ‘ μ€ν¨νλ€. λ§μΈ! μ§μ μ΄λ€. κ°λ¨ν κ°μ§ ꡬ문μ ν μ μλ€.
// Bank
Money reduce(Expression source, String to) {
return Money.dollar(10);
}