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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ProblemThe problem we will solve is representing multi-currency monetary values. It’s not as simple as itmight seem. There are lots of unit-value computations in the world. Any time you use a numberyou generally have at least an implied set of units behind it, whether inches, pixels, or kilograms.Some units can be freely converted into others like pounds to kilograms, others just don’t makeany sense. If you add four meters to two grams you should get an error. One simplifyingassumption that makes unit-values easy to program is that for conversions that make sense, there isa single, immutable conversion ratio. You don’t have to go look up the New York spot quote onpound to kilograms every morning.Money is different. There is no single conversion rate. There isn’t really a single sequence ofconversion rates. Sometimes you want to answer questions like, “How does the value of myportfolio differ if I liquidate it in Hong Kong or Zurich?” This need for flexibility is the primarydriving force in designing objects to represent currency.StartThe first object we’ll give the Simple Superclass Name of Money. It represents a value in a singlecurrency.Class: Moneysuperclass: Objectinstance variables: amount currencyThe variable with the Role Suggesting Instance Variable Name “amount” will hold any oldNumber. This lets us defers issues of numerical accuracy and stability to the Number classes. If youwant a super accurate but slow Money, you can make the Amount a FixedPoint. If you want lessaccuracy but more speed, you can use a Double. As long as the amount responds to number-likemessages, Money won’t care.The variable “currency” will hold Symbol for now, the name of a currency. Currency traders youstandard three letter abbreviations for the currencies of the world, USD for United States Dollars,for example. A complete currency system needs a real Currency object, because differentcurrencies act different computationally, but for now all we care about is whether two currenciesare equal or not.How do we create a Money? We need a Complete Creation Method.Money class>>amount: aNumber currency: aSymbol^self newsetAmount: aNumbercurrency: aSymbolThe method communicates the types of its parameters with Type Suggesting Parameter Names. Thebody of the method uses Indented Control Flow to make it clear to the reader that the CreationParameter Method takes two arguments. Complete Creation Methods also always have anInteresting Return Value.Now we need to set the instance variables to the objects provided to the Complete CreationMethod. We write a Creation Parameter Method:<strong>Coding</strong> <strong>Patterns</strong> page 141 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!