[Newbies] dependency mechanism
smacintyre at mac.com
Thu Jun 1 13:45:55 UTC 2006
I was reading Smalltalk by Example yesterday and I had the exact same
problem as Chris.
I did some exploring of Object's dependency protocol and rewrote the
methods like this:
price := aPrice.
self changed: #price with: self
MyStockWatcher>>update: aSymbol with: aStock
Transcript cr; print: thisContext; tab; print: aSymbol; tab.
aSymbol == #price ifTrue:
nextPutAll: 'Price changed to: '; print: aStock price].
This works like a charm. But I am passing the parent rather then just
the old value because I don't want to store the parent as an instance
variable. Is that a bad thing to do? I'm not sure ... still learning
right from wrong. :) I don't like it as much as what is in the book
because I don't feel I should have to store either the parent or the
old value in an instance variable... it seems less elegant.
Seeing Hilaire's explication of the the events protocol, I read
through the events protocol and rewrote the methods as such:
price := aPrice.
self triggerEvent: #price with: self
aStock when: #price send: #updatePrice: to: self
cr; print: thisContext; tab;
nextPutAll: 'Price changed to: ';
print: aStock price;
This works; but, I am still not passing the old value as well. For
anyone reading this: any comments on style for doing it this way?
On May 31, 2006, at 14:18, Hilaire Fernandes wrote:
> What about using the methods: #triggerEvent:with: and #when:send:to:
> obj when: #price send: #updatePrice: to: anObject2
> obj triggerEvent: #price with: oldPrice
> Object2>>updatePrice: aPrice
> Chris Kassopulo a écrit :
>> The basic dependency example from "Smalltalk By Example" uses
>> Visualworks changed:with and update:with:from: methods like this:
>> price: aPrice
>> | oldPrice |
>> oldPrice := self price.
>> price := aPrice.
>> self changed: #price with: oldPrice
>> update: aSymbol with: oldPrice from: aStock
>> (aSymbol == #price) ifTrue:
>> cr; show: thisContext; tab; show: aSymbol; tab;
>> nextPutAll: 'Price changed from: '; show: oldPrice; tab;
>> nextPutAll: 'To: '; show: aStock price; flush]
>> The problem is that Squeak only implements update:with: so the
>> aStock price messaging doesn't work. My solution was to follow
>> VW's implementation by making the following changes:
>> Modified the changed:with: method to include from:.
>> changed: anAspect with: anObject
>> self dependents do: [:aDependent | aDependent update: anAspect with:
>> anObject from: self]
>> Added an update:with:from: method.
>> update: anAspect with: anObject from: aSender
>> ^ self update: anAspect with: anObject
>> It took a while to see the problem and without looking at VW,
>> I never would have been able to figure a way. Is my fix
>> VW also implements expressInterestIn:for:sendBack: and
>> onChangeSend:to: methods. Anyone familiar with them and care to
>> provide pointers on the possibility of implementing something
>> similar in Squeak?
>> Beginners mailing list
>> Beginners at lists.squeakfoundation.org
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
Edmonton, Alberta, Canada
More information about the Beginners