SBPP 마지막장 예제 자바로 따라하기

Smalltalk Best Practice Patterns의 Money 예제 자바로 따라하기

SBPP의 과정을 그대로 따라보았습니다.

class Money {
    private int amount;
    private String currency;

    public Money() {
    }

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

    public String toString() {
        return amount + " " + currency;
    }

    public Money plus(Money aMoney) {
        return aMoney.addMoney(this);
    }

    public Money addMoney(Money aMoney) {
        if (currency.equals(aMoney.currency)) {
            return new Money(amount + aMoney.amount, currency);
        } else {
            List<Money> monies = new LinkedList<Money>();
            monies.add(this);
            monies.add(aMoney);
            return new MoneySum(monies);
        }
    }

    public int amount() {
        return amount;
    }

    public String currency() {
        return currency;
    }

    public Money addMoneySum(MoneySum aMoneySum) {
        return aMoneySum.addMoney(this);
    }

}

class MoneySum extends Money {
    List<Money> monies;

    public MoneySum(List<Money> aCollection) {
        super();
        setMoneis(aCollection);
    }

    public void setMoneis(List<Money> aCollection) {
        monies = aCollection;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        for (Money money : monies) {
            buf.append(money.toString());
            buf.append("+");
        }
        buf.deleteCharAt(buf.length() - 1);
        return buf.toString();
    }

    public Money plus(Money aMoney) {
        return aMoney.addMoneySum(this);
    }

    public Money addMoney(Money aMoney) {
        monies.add(aMoney);
        return this;
    }
}

public class MoneyTest extends TestCase {
    public void testPlus() {
        Money m1 = new Money(5, "USD");
        Money m2 = new Money(7, "USD");
        Object result = m1.addMoney(m2);
        assertEquals("12 USD", result.toString());
    }

    public void testDifPlus() {
        Money m1 = new Money(5, "USD");
        Money m2 = new Money(7, "GBP");
        Object result = m1.addMoney(m2);
        assertEquals("5 USD+7 GBP", result.toString());

    }

    public void testDif3Plus() {
        Money m1 = new Money(5, "USD");
        Money m2 = new Money(7, "GBP");
        Object result = m1.plus(m2.plus(m1));
        assertEquals("5 USD+7 GBP+5 USD", result.toString());

    }
   
    public void testDif3Plus2() {
        Money m1 = new Money(5, "USD");
        Money m2 = new Money(7, "GBP");
        Object result = (m1.plus(m2)).plus(m1.plus(m2));
        assertEquals("7 GBP+5 USD+7 GBP+5 USD", result.toString());

    }
}


----
요령 1: Scrach에서 작업을 하다가 충분히 숙성된 후에 별개의 파일로 분리해 낸다. 키워 나가는 과정을 통해 디자인을 만들어 나갈때에 쓸만한 요령이다.
요령 2: 결국 언어는 거기서 거기다.  자신이 사용하는 언어를 믿는다.

자바가 Static Typing 언어라는 제약은 어쩔수 없습니다만, Static Typing언어의 강점도 막강하니 뭐 쌤쌤이겠죠.

by 밀리네스 | 2008/02/06 17:51 | 트랙백

트랙백 주소 : http://milines.egloos.com/tb/1708964
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
※ 로그인 사용자만 덧글을 남길 수 있습니다.

◀ 이전 페이지          다음 페이지 ▶