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.

we access the value holder using value, it knows to return the string held by the text object within the<br />

value holder. But when we update the value holder we need to wrap the string in a text object.<br />

28.3.6 The “accessing” protocol<br />

Next we shall define the methods in the “accessing” protocol. There are four methods in this protocol.<br />

We shall first consider those associated with setting <strong><strong>an</strong>d</strong> updating the address book dictionary. The first<br />

of these is used to initialize the inst<strong>an</strong>ce variable addressBook <strong><strong>an</strong>d</strong> the second to add new name <strong><strong>an</strong>d</strong><br />

address associations to the addressBook. These are addressBook: <strong><strong>an</strong>d</strong> newAddress:for:<br />

respectively. The addressBook: method is very straight forward <strong><strong>an</strong>d</strong> is presented below.<br />

addressBook: aDictionary<br />

"This is a n update method for the addressBook inst<strong>an</strong>ce variable. It is used to<br />

accept a new dictionary object to use for the addressBook. It is intended only for<br />

use with the initialization method of the AddressBook class."<br />

addressBook := aDictionary<br />

The newAddress:for: method takes two parameters, the new address <strong><strong>an</strong>d</strong> the name. The method<br />

uses two different Dictionary methods. The first is used to check to see if there is already <strong>an</strong><br />

addressBook entry with that name. We could not use the basic at: message as that generates <strong>an</strong><br />

error if the key provided with the at: message is not present in the dictionary. However, the<br />

at:ifAbsent: message allows the code block provided with the ifAbsent: parameter to be run if<br />

the key is not present. In our case we do not w<strong>an</strong>t it to do <strong>an</strong>ything other th<strong>an</strong> return nil. By default<br />

when a block is evaluated it returns nil if nothing else is returned. We therefore only need to provide<br />

<strong>an</strong>y empty block which will always return the value nil. We c<strong>an</strong> then test the value of the temporary<br />

variable alreadyThere to see if it is nil. If it is nil then we c<strong>an</strong> add the new name <strong><strong>an</strong>d</strong> address to<br />

the dictionary held in addressBook. If it is not nil we c<strong>an</strong> warn the user that <strong>an</strong> entry with that key<br />

already exists using a dialog window.<br />

newAddress: <strong>an</strong>Address for: aName<br />

"This method is used to add a new address to the address book."<br />

| alreadyThere |<br />

alreadyThere := addressBook at: aName ifAbsent: [].<br />

alreadyThere isNil<br />

ifTrue: [addressBook at: aName put: <strong>an</strong>Address]<br />

ifFalse: [Dialog warn: 'That key is already present']<br />

We shall now consider the last two methods in this protocol. These methods are addressBook which<br />

returns the contents of the addressBook inst<strong>an</strong>ce variable <strong><strong>an</strong>d</strong> addressFor: which returns the<br />

address associated with a particular name (if one is present). The addressBook method is:<br />

addressBook<br />

"This is <strong>an</strong> access method for the addressBook inst<strong>an</strong>ce variable"<br />

^addressBook<br />

The addressFor: method is slightly more complex as it must h<strong><strong>an</strong>d</strong>le the situation where <strong>an</strong><br />

address is not available (it uses the at:ifAbsent: message to do this).<br />

addressFor: aName<br />

"This method is used to retrieve <strong>an</strong> address from the address book."<br />

| <strong>an</strong>Entry |<br />

<strong>an</strong>Entry := addressBook at: aName ifAbsent: [nil].<br />

^<strong>an</strong>Entry<br />

28.3.7 A working application<br />

If you have followed all the steps above correctly you should now have a working application. To start<br />

the application, type the following into a Workspace <strong><strong>an</strong>d</strong> evaluate it:<br />

VisualOrg<strong>an</strong>izer open<br />

This will display the window illustrated in Figure 28.2. Now select the Addresses option <strong><strong>an</strong>d</strong> you should<br />

see a window such as that displayed in Figure 28.3. You c<strong>an</strong> now type in a few names <strong><strong>an</strong>d</strong> addresses<br />

selecting input between each. Next try out the query option by typing in a name <strong><strong>an</strong>d</strong> selecting query. If<br />

242

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

Saved successfully!

Ooh no, something went wrong!