Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
• Inherit<strong>an</strong>ce (class to class relationships)<br />
• Inst<strong>an</strong>tiation (class to inst<strong>an</strong>ce)<br />
• Part of or contains (inst<strong>an</strong>ce to inst<strong>an</strong>ce relationships)<br />
However, there is <strong>an</strong>other import<strong>an</strong>t relationship supported by <strong>Smalltalk</strong>, this is the depende ncy<br />
relationship. This is where the state or behavior of one object is dependent on the state of <strong>an</strong>other object.<br />
For example, Figure 25.1 indicates that there are a set of dependency relationships between the objects<br />
A to F.<br />
B<br />
C<br />
A<br />
E<br />
D<br />
Tail depends<br />
on head<br />
F<br />
Figure 25.1: Dependency between objects<br />
This figure illustrates that object A is dependent on some aspect of objects B <strong><strong>an</strong>d</strong> D. In turn object B<br />
is dependent on some aspect of object C <strong><strong>an</strong>d</strong> so on.<br />
In <strong>Smalltalk</strong> dependency is a relationship which c<strong>an</strong> be used to relate two objects such that, as the<br />
state of one ch<strong>an</strong>ges, the state of <strong>an</strong>other automatically ch<strong>an</strong>ges in <strong>an</strong> appropriate m<strong>an</strong>ner. In such a<br />
relationship we say that one object is dependent upon <strong>an</strong>other.<br />
25.3.1 Why do we w<strong>an</strong>t it?<br />
The reasons for dependency are all down to ch<strong>an</strong>ge. That is, we wish to communicate the fact that one<br />
object has ch<strong>an</strong>ged its value to <strong>an</strong>other object which may be interested in either the fact of the ch<strong>an</strong>ge or<br />
the new value effected by the ch<strong>an</strong>ge. The dependency mech<strong>an</strong>ism provides a way of communicating<br />
such events in a generic implementation independent m<strong>an</strong>ner.<br />
An obvious question is “why not just get <strong>an</strong> object to send messages to those interested in it?” The<br />
<strong>an</strong>swer to this is that if you know what objects you w<strong>an</strong>t to send messages to, then do so. However, if all<br />
you know is that sometime, at a later date, some object may need to know something about the state of<br />
<strong>an</strong> object (but at present we don’t know what that other object might be) then we c<strong>an</strong>not arr<strong>an</strong>ge to send<br />
messages to that object - because we don’t know what it will be. However, the dependency mech<strong>an</strong>ism<br />
allows <strong>an</strong>y object (whose class is a subclass of <strong>Object</strong>) to be involved in a dependency. A second<br />
adv<strong>an</strong>tage is that we don’t have to know about the objects interested in the object we are working on.<br />
All we need to know is that it might be involved in a dependency relationship <strong><strong>an</strong>d</strong> we c<strong>an</strong> let the<br />
(hidden) dependency mech<strong>an</strong>ism take care of informing these unknown objects about the updates.<br />
25.3.2 How does it work?<br />
As is hinted at in the above section, the dependency mech<strong>an</strong>ism is implemented in the class <strong>Object</strong>.<br />
This of course me<strong>an</strong>s that, as all classes in <strong>Smalltalk</strong> are subclasses of <strong>Object</strong>, all objects c<strong>an</strong> be<br />
involved in a dependency. You c<strong>an</strong> browse the <strong>Object</strong> class to explore the dependency mech<strong>an</strong>ism. If<br />
you use the implementors option off the message window menu you will find that some of the messages<br />
are redefined lower down in the class hierarchy. This is purely for internal efficiency or specific<br />
implementation reasons - you will still use the mech<strong>an</strong>ism in the same way.<br />
The basic implementation, inherited from <strong>Object</strong>, associates a collection of other objects with <strong>an</strong><br />
object. This collection holds the objects whic h are dependent on the object (collectively these objects<br />
are known as the objects dependents ). For example, in Figure 25.2 the object <strong>Object</strong>A has two<br />
dependents <strong>Object</strong>B <strong><strong>an</strong>d</strong> <strong>Object</strong>C. The links to the dependent objects are held separately from<br />
<strong>Object</strong>A in a dependents list. <strong>Object</strong>A c<strong>an</strong> access this list via the message dependents.<br />
207