Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
While this may seem to be a complicated (<strong><strong>an</strong>d</strong> rat her convoluted) way to ch<strong>an</strong>ge the value of part of<br />
the model, it has m<strong>an</strong>y adv<strong>an</strong>tages in flexibility <strong><strong>an</strong>d</strong> modularity. No longer does the model have to<br />
inform the interface (view) about <strong>an</strong>y updates; the ValueHolder does it automatically wh enever the<br />
value it holds ch<strong>an</strong>ges. Indeed, it is no longer necessary for the model to concern itself with <strong>an</strong>y aspect<br />
of the view (or controller) at all. The model now really is separated from the view <strong><strong>an</strong>d</strong> controller. This<br />
makes the task of developing highly graphical systems much easier.<br />
27.9.2 Working with value holders<br />
If the user interface builder described above is used to construct a window <strong><strong>an</strong>d</strong> the definer has been used<br />
to generate the inst<strong>an</strong>ce variables <strong><strong>an</strong>d</strong> accessor methods for the windows aspects, then valu e holders for<br />
these inst<strong>an</strong>ce variables will automatically be created. However, it is still possible to do this m<strong>an</strong>ually<br />
(<strong><strong>an</strong>d</strong> may be used with non interface inst<strong>an</strong>ce variables).<br />
A new value holder c<strong>an</strong> be created in two ways:<br />
1. By creating <strong>an</strong> inst<strong>an</strong>ce of the class ValueHolder. For example,<br />
bal<strong>an</strong>ce := ValueHolder with: 230.<br />
2. By converting <strong>an</strong> object to a value holder <strong><strong>an</strong>d</strong> object. For example,<br />
bal<strong>an</strong>ce := 230 asValue.<br />
Whichever approach is used, the contents of the value holder is accessed <strong><strong>an</strong>d</strong> up dated in the same<br />
way. Accessing the valueholder’s contents is performed using the value message <strong><strong>an</strong>d</strong> updating it is<br />
performed using the value: message. For example:<br />
currentBal<strong>an</strong>ce := bal<strong>an</strong>ce value.<br />
bal<strong>an</strong>ce value: (currentBal<strong>an</strong>ce + deposit).<br />
If a new value is assigned to a value holder the value: message must be used, otherwise the value<br />
holder will be deleted <strong><strong>an</strong>d</strong> the link to a window (if present) <strong><strong>an</strong>d</strong> to <strong>an</strong>y dependents will be lost.<br />
27.10 The onCh<strong>an</strong>geSend:to: message<br />
In <strong>an</strong> earlier chapter we discussed the use of the dependency mech<strong>an</strong>ism as a me<strong>an</strong>s of informing<br />
interested parties about a ch<strong>an</strong>ge in the value of something. Value holders take this concept one step<br />
further <strong><strong>an</strong>d</strong> makes it more explicit. Whenever the value of a value holder is modified using the value:<br />
message a check is made to see whether there are <strong>an</strong>y interests recorded on that value holder. If there,<br />
then the associated action is performed.<br />
The interest in a value holder is registered using the onCh<strong>an</strong>geSend: aSymbol to:<br />
<strong>an</strong><strong>Object</strong> message. This message states that when the value holders value ch<strong>an</strong>ges (via value:) the<br />
symbol is sent to the object as a message. A common use of this message is to ensure that some action<br />
is performed whenever the user ch<strong>an</strong>ges a value currently being displayed. For example:<br />
bal<strong>an</strong>ce onCh<strong>an</strong>geSend: #checkInTheRed to: currentAccount<br />
states that when the value holder held in bal<strong>an</strong>ce is modified, the message checkInTheRed should<br />
be sent to the object held in currentAccount. Note that unlike the version of the dependency<br />
mech<strong>an</strong>ism discussed earlier, this approach explicitly states what action should be performed on what<br />
object when a particular value ch<strong>an</strong>ges.<br />
If at a later date the dependency between the value holder <strong><strong>an</strong>d</strong> the message send to the specified<br />
object should be retracted, the (less me<strong>an</strong>ingfully named) retractInterestsFor: <strong>an</strong><strong>Object</strong><br />
message c<strong>an</strong> be used.<br />
232