13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

MoneySum>>printOn: aStreammonies do:[:each |aStreamprint: each;nextPutAll: ' + '].aStream skip: -3We used a basketful of patterns in this method: Direct Variable Access, Role Suggesting ParameterName, Simple Enumeration Parameter, Rectangular Block, Indented Control Flow, and Do.With MoneySum in hand, we can modify Money>>+ to return a MoneySum if the two currenciesdon’t match:Money>>+ aMoney^currency = aMoney currencyifTrue:[self speciesamount: amount + aMoney amountcurrency: currency]ifFalse:[MoneySum monies: (Arraywith: selfwith: aMoney)]We need a Getting Method for Money’s currency:Money>>currency^currencyNow our multi-currency example works:| m1 m2 |m1 := Moneyamount: 5currency: #USD.m2 := Moneyamount: 7currency: #GBP.m1 + m2 5 USD + 7 GBPJust to check, the single currency example works, still:| m1 m2 |m1 := Moneyamount: 5currency: #USD.m2 := Moneyamount: 7currency: #USD.m1 + m2 12 USD<strong>Coding</strong> <strong>Patterns</strong> page 144 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!