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.

You c<strong>an</strong> find the methods which implement<br />

dependents, addDependent: <strong><strong>an</strong>d</strong><br />

removeDependent: all defined in the class <strong>Object</strong> in the dependents access protocol (as<br />

illustrated in Figure 25.3). Browse these methods yourself, they will help you to underst<strong><strong>an</strong>d</strong> how the<br />

dependency mech<strong>an</strong>ism works.<br />

25.3.4 A simple dependency example<br />

This subsection presents a very simple dependency example, which we will develop further during the<br />

chapter. It creates two objects <strong><strong>an</strong>d</strong> creates a dependency between them. The objects are inst<strong>an</strong>ces of a<br />

class Data<strong>Object</strong> <strong><strong>an</strong>d</strong> Dependent<strong>Object</strong>. These classes are direct subclasses of <strong>Object</strong>. Create<br />

the classes placing them in <strong>an</strong> appropriate class category (for example, dependency demo). For example:<br />

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

inst<strong>an</strong>ceVariableNames: ''<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'dependency demo'<br />

Then evaluate the following in a Workspace:<br />

| temp1 temp2 |<br />

temp1 := Data<strong>Object</strong> new.<br />

temp2 := Dependent<strong>Object</strong> new.<br />

temp1 addDependent: temp2.<br />

Tr<strong>an</strong>script show: (temp1 dependents) printString.<br />

temp1 inspect.<br />

The result of the show message expression, printed in the Tr<strong>an</strong>script, is:<br />

#(a Dependent<strong>Object</strong>)<br />

Notice that, although the dependent<strong>Object</strong> inst<strong>an</strong>ce was printed in the Tr<strong>an</strong>script, no<br />

dependents c<strong>an</strong> be found for the data<strong>Object</strong> in the inspector. Indeed no inst<strong>an</strong>ce variable called<br />

dependents c<strong>an</strong> be found at all.<br />

From the point of view of the class Data<strong>Object</strong>, dependency is <strong>an</strong> invisible mech<strong>an</strong>ism which<br />

works behind the scenes. Of course, this is not really the case. The dependency mech<strong>an</strong>ism has been<br />

inherited from <strong>Object</strong> <strong><strong>an</strong>d</strong> is implemented via message sends <strong><strong>an</strong>d</strong> method executions just like <strong>an</strong>y<br />

behavior provided by <strong>an</strong> object.<br />

25.3.5 Making dependency work for you<br />

We have now considered how we construct a dependency relationship. However, we w<strong>an</strong>t this<br />

relationship to be used to inform the dependent object(s) that a ch<strong>an</strong>ge has occurred in the object on<br />

which they depend. That is, we w<strong>an</strong>t to tell one object that <strong>an</strong>other object, has ch<strong>an</strong>ged in some way.<br />

To do this we use two sets of methods. One set is used to state that something has ch<strong>an</strong>ged. These<br />

are called “ch<strong>an</strong>ged” methods. The other set are used to state what type of update is required. These are<br />

called “update” methods. They work as illustrated in Figure 25.4.<br />

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

message<br />

dependents<br />

<strong>Object</strong>A<br />

update<br />

message<br />

update<br />

message<br />

<strong>Object</strong>B<br />

<strong>Object</strong>C<br />

Figure 25.4: The dependency mech<strong>an</strong>ism in action<br />

Figure 25.4 illustrates the sequence of messages which are sent in response to a ch<strong>an</strong>ge message<br />

being sent to <strong>an</strong> obje ct. That is, when <strong>Object</strong>A is sent a ch<strong>an</strong>ged message (usually by itself) all its<br />

dependents are sent <strong>an</strong> update message. Again from the point of view of <strong>Object</strong>A much of this<br />

behavior is hidden. In fact so much so that a point of confusion relates to the sending of one message<br />

209

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

Saved successfully!

Ooh no, something went wrong!