fork download
  1. Object extend [ as: similarClass [ ^similarClass newFrom: self ] ]
  2.  
  3. Object subclass: PrimePrice [
  4. | value |
  5. value [ ^value ]
  6. value: newValue [ value := newValue ]
  7. ]
  8.  
  9. PrimePrice class extend [
  10. value: value [ ^self new value: value; yourself ]
  11. ]
  12.  
  13. Object subclass: MarginPrice [
  14. | originalPrice |
  15. setOriginalPrice: price [ originalPrice := price ]
  16. value: newValue [ originalPrice value: newValue ]
  17. ]
  18.  
  19. MarginPrice class extend [
  20. newFrom: price [ ^self new setOriginalPrice: price; yourself ]
  21. ]
  22.  
  23. MarginPrice subclass: DoublePrice [ value [ ^originalPrice value * 2 ] ]
  24.  
  25. MarginPrice subclass: WholesalePrice [
  26. | advantage |
  27. value [ ^originalPrice value + advantage ]
  28. advantage: newAdvantage [ advantage := newAdvantage ]
  29. ]
  30.  
  31. | price |
  32. price := ((((((PrimePrice value: 120)
  33. as: DoublePrice)
  34. as: WholesalePrice) advantage: 80)
  35. as: DoublePrice)
  36. as: WholesalePrice) advantage: 200.
  37. price value displayNl.
  38. (price value: 100; value) displayNl
Success #stdin #stdout 0.01s 335488KB
stdin
Standard input is empty
stdout
840
760