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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

These messages are sent automatically by the dependency mech<strong>an</strong>ism in response to a ch<strong>an</strong>ge d<br />

message being sent to the object, on which the object they are sent to depends. That is, if <strong>Object</strong>A is<br />

sent a ch<strong>an</strong>ged message, one of the above will be sent to <strong>Object</strong>B. Which message appears to be sent<br />

depends on which method has been defined in <strong>Object</strong>B. This is because the method calling process is<br />

implemented such that the first message sent to the object will be update:with:from:. Unless this<br />

is overwritten, this method calls update:with:. In turn the update:with: method calls the<br />

update: method. This method by default does nothing. Therefore, if you define <strong>an</strong>y one of these three<br />

methods, that method will be executed.<br />

This me<strong>an</strong>s that the developer c<strong>an</strong> decide how much information the dependent object wishes to<br />

work with. The parameter values that the developer c<strong>an</strong> work with are:<br />

• <strong>an</strong>Aspect the value used for <strong>an</strong>Aspect in the ch<strong>an</strong>ged message or nil.<br />

• aParameter the value used for aParameter in the ch<strong>an</strong>ged message or nil.<br />

• <strong>an</strong><strong>Object</strong> the object which received the ch<strong>an</strong>ged message.<br />

25.3.8 Extending the dependency example<br />

This section will provide <strong>an</strong> example of how the dependency mech<strong>an</strong>ism works. We shall use the<br />

Data<strong>Object</strong> <strong><strong>an</strong>d</strong> Dependent<strong>Object</strong> classes defined back in section 25.3.4.<br />

The first thing we shall do is to define a couple of inst<strong>an</strong>ce var iables in Data<strong>Object</strong>. These<br />

variables will be age, name <strong><strong>an</strong>d</strong> address. For example:<br />

<strong>Object</strong> subclass: #Data<strong>Object</strong><br />

inst<strong>an</strong>ceVariableNames: 'age name address '<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'dependency demo'<br />

Next we shall define <strong>an</strong> updater method for each of these inst<strong>an</strong>ce variables. For example, in Figure<br />

25.5, we have defined a new method age: which sets the age inst<strong>an</strong>ce variable. It then informs the<br />

object that we have ch<strong>an</strong>ged its age <strong><strong>an</strong>d</strong> that this fact should be passed onto its dependents (i.e. self<br />

ch<strong>an</strong>ged: #age). This is a typical usage of a ch<strong>an</strong>ged message. That is, it is sent to self informing<br />

self about the ch<strong>an</strong>ge which has taken place. It is very poor style to have one object send <strong>an</strong> inst <strong>an</strong>ce<br />

of Data<strong>Object</strong> the message age:, followed by the ch<strong>an</strong>ged message!<br />

Next we shall define how <strong>an</strong> inst<strong>an</strong>ce of Dependent<strong>Object</strong> should respond to the ch<strong>an</strong>ge in a<br />

Data<strong>Object</strong>. That is we will define one of the update methods. The method we will define is the<br />

update: method. This method is placed in a protocol called updating. The actual method is illustrated<br />

in Figure 25.6. As this is just a simple example, all that this does is to print a string in the<br />

Tr<strong>an</strong>script which illustrates the ch<strong>an</strong>ge which has occurred to a Data<strong>Object</strong> inst<strong>an</strong>ce.<br />

Figure 25.5: Defining <strong>an</strong> updater for age<br />

We are now ready to try out this simple example. To do this we will use the source code previously<br />

typed into the Tr<strong>an</strong>script (minus the last inspect statement). To this we will add three statements which<br />

set the data<strong>Object</strong>’s age, name <strong><strong>an</strong>d</strong> address:<br />

211

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

Saved successfully!

Ooh no, something went wrong!