I was thinking about the code in Pharo.
Plenty of examples:
- Converting money
can you tell me more about this one?
I mean summing and converting different money. Here is the whole idea: 1 EUR = 662 CLP (Chilean pesos) You have a class Money to which you can sum other money. Object subclass: #Money instVarNames: âvalueâ âI omit here the accessors of value. Note that generating the accessors of value produces a method value1, you have to rename it" Money>>+ anotherMoney self subclassResponsibility Money>>sumWithEUR: money self subclassResponsibility Money>>sumWithCLP: money self subclassResponsibility Money>>= anotherMoney ^ self class == anotherMoney class and: [ self value = anotherMoney value ] Money >>printOn: str "Useful for debugging" super printOn: str. str nextPut: $<. str nextPutAll: self value asString. str nextPut: $>. You have two subclasses: Money subclass: #EUR Money subclass: #CLP EUR>>+ anotherMoney ^ anotherMoney sumWithEUR: self EUR>>sumWithEUR: money ^ EUR new value: self value + money value EUR>>sumWithCLP: money ^ CLP new value: (self value * 662) + money value CLP>>+ anotherMoney ^ anotherMoney sumWithCLP: self CLP>>sumWithEUR: money ^ EUR new value: (self value / 662) + money value CLP>>sumWithCLP: money ^ CLP new value: self value + money value Here is a test: TestCase subclass: #MoneyTest MoneyTest>>testSum | clp1 eur1 clp2 eur2 | clp1 := CLP new value: 3500. eur1 := EUR new value: 10. clp2 := CLP new value: 5000. eur2 := EUR new value: 20. self assert: clp1 + clp2 equals: (CLP new value: 8500). self assert: clp1 + eur1 equals: (CLP new value: 3500 + 6620). self assert: eur1 + eur2 equals: (EUR new value: 30). self assert: eur1 + clp2 equals: (EUR new value: 5000 / 662 + 10). To be really complete, the example also needs to implement hash and the test should also test equality. I hope it helps! Cheers, Alexandre
- Paper, Stone, Scissor - A canvas containing triangle, circle, box has to be printed, on an html canvas or PDF. (Very close to the Visitor design pattern, but simpler)
Here are the lectures I use: https://dl.dropboxusercontent.com/u/31543901/TMP/DoubleDispatch.zip
TX here is what I wrote today
(One of them is highly inspired from a lecture from Oscar)
I will have a look now
Hope it helps!
Cheers, Alexandre
On Sep 13, 2016, at 7:33 AM, stepharo <stepharo@free.fr> wrote:
Hi
If you happen to know a double dispatch situation in Pharo, I'm interested since I'm revisiting my lecture.
Stef
<Design-DoubleDispatch.pdf>