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.

dependents<br />

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

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

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

Figure 25.2: An object <strong><strong>an</strong>d</strong> its dependents<br />

The message dependents c<strong>an</strong> be used to obtain a list of all the dependents of <strong>an</strong> object (no matter<br />

what the class of the object). For example, to obtain a list of the dependents on <strong>Object</strong>A we could use:<br />

<strong>Object</strong>A dependents.<br />

From the point of view o f the actual implementation, the dependents are sometimes held in <strong>an</strong><br />

inst<strong>an</strong>ce variable inherited from above (so you c<strong>an</strong> see it in <strong>an</strong> inspector). This is the case for most of<br />

the user interface classes. In other cases the dependents list is held as part of<br />

<strong>an</strong><br />

IdentityDictionary in a class variable (this is the default mech<strong>an</strong>ism inherited from <strong>Object</strong>).<br />

25.3.3 Constructing dependencies<br />

The addDependent: message is used to add <strong>an</strong> object to <strong>an</strong>other object’s dependency list. For<br />

example, we c<strong>an</strong> construct the above dependencies using:<br />

<strong>Object</strong>A addDependent: <strong>Object</strong>B.<br />

<strong>Object</strong>A addDependent: <strong>Object</strong>C.<br />

Note that <strong>an</strong> object holds (or c<strong>an</strong> access) a list of objects which depend on it. Whereas <strong>an</strong> object<br />

c<strong>an</strong>not access information about the objects on which it depends. For example, there are no references<br />

from <strong>Object</strong>B or <strong>Object</strong>C back to <strong>Object</strong>A in Figure 25.2. Thus <strong>an</strong> object does not hold a list of<br />

objects on which it depends. This may seem a bit str<strong>an</strong>ge at first, however, o nce you gain <strong>an</strong><br />

underst<strong><strong>an</strong>d</strong>ing of how the dependency mech<strong>an</strong>ism works, hopefully you will see why things are this<br />

way round.<br />

It is also possible to remove dependencies once they have been created. This c<strong>an</strong> be done using the<br />

removeDependent: message. For example:<br />

<strong>Object</strong>A removeDependent: <strong>Object</strong>B.<br />

This removes <strong>Object</strong>B from the dependency list of <strong>Object</strong>A. It should be noted that as <strong>an</strong><br />

IdentityDictionary is used (at least in objects which are inst<strong>an</strong>ces of classes below <strong>Object</strong>),<br />

<strong>Object</strong>B must be the same object as was used to create the dependency. It c<strong>an</strong>not be merely<br />

equivalent.<br />

Figure 25.3: The dependents message defined in <strong>Object</strong><br />

208

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

Saved successfully!

Ooh no, something went wrong!