29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(i.e. the ch<strong>an</strong>ged message) <strong><strong>an</strong>d</strong> the execution of <strong>an</strong>other method (i.e. the update method). That is, a<br />

programmer defining <strong>Object</strong>s A, B <strong><strong>an</strong>d</strong> C will:<br />

1. send a ch<strong>an</strong>ged message to <strong>Object</strong>A<br />

2. define <strong>an</strong> update method in <strong>Object</strong>B <strong><strong>an</strong>d</strong> <strong>Object</strong>C.<br />

The confusion here stems from the need to send one message but define <strong>an</strong>other. However, if you think<br />

about how you are linking into the existing dependency framework, it c<strong>an</strong> make more sense. Essentially<br />

the ch<strong>an</strong>ge message is a message to the dep endency mech<strong>an</strong>ism asking it to notify the object’s<br />

dependents about a ch<strong>an</strong>ge. The dependency mech<strong>an</strong>ism is inherited <strong><strong>an</strong>d</strong> is generic across applications.<br />

However, there is no way that the system developers could know when the ch<strong>an</strong>ge message should be<br />

sent. That is application specific. It is therefore the application developer’s responsibility to send the<br />

ch<strong>an</strong>ged message. For example, you may only w<strong>an</strong>t dependents to get told of certain ch<strong>an</strong>ges, such as<br />

updates to only one field on <strong>an</strong> input screen etc.<br />

In turn , there is no way that the system developers could have known how you would w<strong>an</strong>t the<br />

dependents to update themselves. The update message could indicate that the new value produced by<br />

the originating object should be displayed, that some calculation should be re -performed, or that a<br />

database should be accessed. In fact in the default implementation, inherited from <strong>Object</strong>, the update<br />

methods do nothing. They are only there so that <strong>an</strong> error is not generated if a message is sent to <strong>an</strong><br />

object which does not have a new update method defined for it.<br />

In the simple <strong>Object</strong>A, <strong>Object</strong>B <strong><strong>an</strong>d</strong> <strong>Object</strong>C example above, we would need to specify what<br />

<strong>Object</strong>B <strong><strong>an</strong>d</strong> <strong>Object</strong>C should do when <strong>Object</strong>A ch<strong>an</strong>ged. This would require defining our own<br />

update methods.<br />

25.3.6 The “ch<strong>an</strong>ged” methods<br />

There are three different ch<strong>an</strong>ged messages which take zero, one or two parameters. They are:<br />

<strong>an</strong><strong>Object</strong> ch<strong>an</strong>ged: <strong>an</strong>Aspect with: aParameter.<br />

<strong>an</strong><strong>Object</strong> ch<strong>an</strong>ged: <strong>an</strong>Aspect.<br />

<strong>an</strong><strong>Object</strong> ch<strong>an</strong>ged.<br />

The first point to note about the ch<strong>an</strong>ged messages are that they are sent to the object which has<br />

ch<strong>an</strong>ged in some way. That is, they are used to inform the object that a ch<strong>an</strong>ge has taken place to it <strong><strong>an</strong>d</strong><br />

that this ch<strong>an</strong>ge should be passed onto <strong>an</strong>y dependents it may have. That is, the ch<strong>an</strong>ged message does<br />

not effect the ch<strong>an</strong>ge, nor is it sent to the objects which wish to be notified about the ch<strong>an</strong>ge, rather it is<br />

telling the ch<strong>an</strong>ged object that it should notify the dependency mech<strong>an</strong>ism of the ch<strong>an</strong>ge.<br />

Each of the three messages will trigger off the update part of the dependency mech<strong>an</strong>ism. The only<br />

difference between the messages relates to the amount of information provided. The first message c<strong>an</strong><br />

be used to not only tell the dependent objects what aspect of the object ch<strong>an</strong>ged but also what the new<br />

value produced by the ch<strong>an</strong>ge was. For example, the aspect that has been modified might be <strong>an</strong> inst<strong>an</strong>ce<br />

variable <strong><strong>an</strong>d</strong> the parameter the new value of the inst<strong>an</strong>ce variable. In turn the second message only tells<br />

the dependents what aspect has ch<strong>an</strong>ged, while the third only in forms the dependents that some ch<strong>an</strong>ge<br />

has taken place. Therefore the simplest ch<strong>an</strong>ged message (<strong><strong>an</strong>d</strong> the one with the least information) is the<br />

ch<strong>an</strong>ged message. This c<strong>an</strong> be useful when you w<strong>an</strong>t to make sure that the dependents assume<br />

nothing about the object to which the ch<strong>an</strong>ge is happening.<br />

A point to note is that the way that these messages are implemented is that the ch<strong>an</strong>ged method calls<br />

the ch<strong>an</strong>ged: method with nil as a parameter. In turn this method calls the ch<strong>an</strong>ged:with:<br />

method with the parameter pass ed to it <strong><strong>an</strong>d</strong> a nil parameter. Thus if you wish to modify the way that<br />

the dependency mech<strong>an</strong>ism works, the ch<strong>an</strong>ged side is encapsulated within the ch<strong>an</strong>ged:with:<br />

method.<br />

25.3.7 The “update” methods<br />

There are three different versions of the update messages which take one, two or three parameters. They<br />

are:<br />

update: <strong>an</strong>Aspect with: aParameter from: <strong>an</strong><strong>Object</strong><br />

update: <strong>an</strong>Aspect with: aParameter<br />

update: <strong>an</strong>Aspect<br />

210

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

Saved successfully!

Ooh no, something went wrong!