본문으둜 κ±΄λ„ˆλ›°κΈ°

🌈 Chapter 8: 객체 λ§Œλ“€κΈ°

두 times() κ΅¬ν˜„ μ½”λ“œκ°€ 거의 λ˜‘κ°™λ‹€.

// Franc
Franc times(int multiplier) {
return new Franc(amount * multiplier);
}

// Dollar
Dollar times(int multiplier) {
return new Dollar(amount * multiplier);
}

μ–‘μͺ½ λͺ¨λ‘ Moneyλ₯Ό λ°˜ν™˜ν•˜κ²Œ λ§Œλ“€λ©΄ 더 λΉ„μŠ·ν•˜κ²Œ λ§Œλ“€ 수 μžˆλ‹€.

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

// Dollar
Money times(int multiplier) {
return new Dollar(amount * multiplier);
}

Money의 두 ν•˜μœ„ ν΄λž˜μŠ€λŠ” 그닀지 λ§Žμ€ 일을 ν•˜λŠ” 것 같지 μ•ŠμœΌλ―€λ‘œ μ•„μ˜ˆ μ œκ±°ν•΄λ²„λ¦¬κ³  μ‹Άλ‹€. 그런데 ν•œλ²ˆμ— κ·Έλ ‡κ²Œ 큰 단계λ₯Ό λ°ŸλŠ” 것은 TDDλ₯Ό 효과적으둜 보여주기에 μ μ ˆν•˜μ§€ μ•Šμ„ 것 κ°™λ‹€.

그렇지. ν•˜μœ„ ν΄λž˜μŠ€μ— λŒ€ν•œ 직접적인 μ°Έμ‘°κ°€ 적어진닀면 ν•˜μœ„ 클래슀λ₯Ό μ œκ±°ν•˜κΈ° μœ„ν•΄ ν•œ 발짝 더 λ‹€κ°€μ„°λ‹€κ³  ν•  수 μžˆκ² λ‹€. Money에 Dollarλ₯Ό λ°˜ν™˜ν•˜λŠ” νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό λ„μž…ν•  수 μžˆλ‹€. 이런 μ‹μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.

public void testMultiplication() {
Dollar five = Money.dollar(5);
assertEquals(new Dollar(10), five.times(2));
assertEquals(new Dollar(15), five.times(3));
}

κ΅¬ν˜„ μ½”λ“œλŠ” Dollarλ₯Ό μƒμ„±ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€.

static Dollar dollar(int amount) {
return new Dollar(amount);
}

Dollar에 λŒ€ν•œ μ°Έμ‘°κ°€ 사라지길 λ°”λΌλ―€λ‘œ ν…ŒμŠ€νŠΈμ˜ μ„ μ–ΈλΆ€λ₯Ό λ‹€μŒκ³Ό 같이 λ°”κΏ”μ•Όν•œλ‹€.

public void testMultiplication() {
Money five = Money.dollar(5);
assertEquals(new Dollar(10), five.times(2));
assertEquals(new Dollar(15), five.times(3));
}

μ»΄νŒŒμΌλŸ¬κ°€ Moneyμ—λŠ” times()κ°€ μ •μ˜λ˜μ§€ μ•Šμ•˜λ‹€λŠ” 사싀을 μ•Œλ €μ€€λ‹€. μ§€κΈˆμ€ κ·Έκ±Έ κ΅¬ν˜„ν•  μ€€λΉ„κ°€ λ˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ—, Moneyλ₯Ό 좔상 클래슀둜 λ³€κ²½ν•œ ν›„ Money.times()λ₯Ό μ„ μ–Έν•˜μž.

abstract class Money
abstract Money times(int multiplier);

이제 νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό 선언을 λ°”κΏ€ 수 μžˆλ‹€.

static Money dollar(int amount) {
return new Dollar(amount);
}

λͺ¨λ“  ν…ŒμŠ€νŠΈκ°€ μ‹€ν–‰λ˜λ―€λ‘œ μ΅œμ†Œν•œ λ­”κ°€λ₯Ό κΉ¨νŠΈλ¦¬μ§„ μ•Šμ•˜λ‹€. 이제 νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό ν…ŒμŠ€νŠΈ μ½”λ“œμ˜ λ‚˜λ¨Έμ§€ λͺ¨λ“  κ³³μ—μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.

public void testMultiplication() {
Money five = Money.dollar(5);
assertEquals(Money.Dollar(10), five.times(2));
assertEquals(Money.Dollar(15), five.times(3));
}

public void testEquality() {
assertTrue(Money.dollar(5).equals(Money.dollar(5)));
assertFalse(Money.Dollar(5).equals(Money.Dollar(6)));
assertTrue(new Franc(5).equals(new Franc(5)));
assertFalse(new Franc(5).equals(new Franc(6)));
assertFalse(new Franc(5).equals(Money.Dollar(5)));
}

이제 전보닀 쑰금 λ‚˜μ€ 상황이닀. μ–΄λ–€ ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œλ„ DollarλΌλŠ” μ΄λ¦„μ˜ ν•˜μœ„ ν΄λž˜μŠ€κ°€ μžˆλ‹€λŠ” 사싀을 μ•Œμ§€ λͺ»ν•œλ‹€. ν•˜μœ„ 클래슀의 쑴재λ₯Ό ν…ŒμŠ€νŠΈμ—μ„œ λΆ„λ¦¬ν•¨μœΌλ‘œμ¨ μ–΄λ–€ λͺ¨λΈ μ½”λ“œμ—λ„ 영ν–₯을 주지 μ•Šκ³  상속 ꡬ쑰λ₯Ό λ§ˆμŒλŒ€λ‘œ λ³€κ²½ν•  수 있게 됐닀.

testFrancMultiplication을 λ³€κ²½ν•˜λ €κ³  ν•˜λ‹€λ³΄λ‹ˆ 이 ν…ŒμŠ€νŠΈκ°€ κ²€μ‚¬ν•˜λŠ” 둜직 쀑 Dollar κ³±ν•˜κΈ° ν…ŒμŠ€νŠΈμ— μ˜ν•΄ κ²€μ‚¬λ˜μ§€ μ•ŠλŠ” 뢀뢄은 ν•˜λ‚˜λ„ μ—†λ‹€λŠ” 것을 μ•Œμ•„μ±˜λ‹€. 이 ν…ŒμŠ€νŠΈλ₯Ό μ‚­μ œν•œλ‹€λ©΄ μ½”λ“œμ— λŒ€ν•œ 확신이 μ‘°κΈˆμ΄λΌλ„ μ€„μ–΄λ“€κΉŒ? μ•½κ°„μ΄λ‚˜λ§ˆ 그럴 κ°€λŠ₯성이 μžˆμœΌλ‹ˆ 일단 κ·ΈλŒ€λ‘œ λ‚¨κ²¨λ‘μž. ν•˜μ§€λ§Œ μ’€ μˆ˜μƒμ©λ‹€.

public void testEquality() {
assertTrue(Money.dollar(5).equals(Money.dollar(5)));
assertFalse(Money.Dollar(5).equals(Money.Dollar(6)));
assertTrue(Money.Franc(5).equals(Money.Franc(5)));
assertFalse(Money.Franc(5).equals(Money.Franc(6)));
assertFalse(Money.Franc(5).equals(Money.Dollar(5)));
}

public void testFrancMultiplication() {
Money five = Money.franc(5);
assertEquals(Money.franc(10), five.times(2));
assertEquals(Money.franc(15), five.times(3));
}

κ΅¬ν˜„μ€ Money.dollar()와 μœ μ‚¬ν•˜λ‹€.

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

λ‹€μŒ μž₯μ—μ„œ μš°λ¦¬λŠ” times()의 쀑볡을 거둬낼 것이닀.