fork(1) download
  1. "the original definition of class Price"
  2.  
  3. Object subclass: Price [
  4. | value |
  5. init [ value := 0 ]
  6. value [ ^value ]
  7. value: newValue [ value := newValue ]
  8. ]
  9.  
  10. Price class extend [
  11. new [ ^ super new init; yourself ]
  12. value: value [ ^self new value: value; yourself ]
  13. ]
  14.  
  15.  
  16. "tweaks for accepting decorations"
  17.  
  18. Price extend [
  19. | decorators |
  20. decorators [ ^decorators ifNil: [ decorators := OrderedCollection new ] ]
  21. value [ ^self decorators inject: value into: [ :acc :deco | deco value: acc ] ]
  22. ]
  23.  
  24.  
  25. "usage example"
  26.  
  27. | price |
  28. price := Price value: 120.
  29. price decorators
  30. add: [:x | x * 2];
  31. add: [:x | x + 80];
  32. add: [:x | x * 2];
  33. add: [:x | x + 200].
  34. price value displayNl
Success #stdin #stdout 0.01s 335424KB
stdin
Standard input is empty
stdout
840