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 c<strong>an</strong>not tell from this line alone, what action the object will perform. The mech<strong>an</strong>ism essentially<br />

allows the method name, as well as the object, to be held in a variable. For example,<br />

is equivalent to:<br />

aVariable := #size.<br />

aCollection perform: aVariable.<br />

aCollection size.<br />

The contents of the variable passed with the perform: message must be a symbol, otherwise <strong>an</strong><br />

error will be generated. It must also be a legal message otherwise <strong>an</strong> error will also be generated. For<br />

example, if the variable contained the symbol #abcd, then that would be equivalent to e valuating<br />

aCollection abcd. Unless this message is defined for this class of object, then the message will<br />

generate a “not understood” exception (whether it is sent directly or as part of a perform message).<br />

The perform mech<strong>an</strong>ism c<strong>an</strong> also h<strong><strong>an</strong>d</strong>le methods which take arguments. The arguments are h<strong><strong>an</strong>d</strong>led<br />

by different versions of the perform: message:<br />

1. for methods with one argument use perform:with:<br />

2. for methods with two arguments use perform:with:with:<br />

3. for methods with three arguments use perform:with:with:with:<br />

4. for methods with more th<strong>an</strong> three arguments use perform:withArguments: where the<br />

arguments are assumed to be in <strong>an</strong> array.<br />

For example,<br />

<strong>an</strong><strong>Object</strong> perform: aSymbol with: <strong>an</strong>other<strong>Object</strong>.<br />

<strong>an</strong><strong>Object</strong> perform: aSymbol with: object1 with: object2.<br />

<strong>an</strong><strong>Object</strong> perform: aSymbol with: object1 with: object2 with: object3.<br />

<strong>an</strong><strong>Object</strong> perform: aSymbol withArguments: aCollection<br />

Note that the symbol held in aSymbol will be the message selector name. That is, if the message to<br />

be sent would normally be written as:<br />

<strong>an</strong><strong>Object</strong> at: 2 out: 5.<br />

then with the perform mech<strong>an</strong>ism this would be written as:<br />

aSymbol := #at:put:<br />

object1 := 2.<br />

object2 := 5.<br />

<strong>an</strong><strong>Object</strong> perform: aSymbol with: object1 with: object2.<br />

The perform mech<strong>an</strong>ism is very powerful <strong><strong>an</strong>d</strong> provides a great deal of flexibi lity. However, in most<br />

normal situations you should not need to use it. Indeed, there are a number of points to remember when<br />

thinking about using the perform mech<strong>an</strong>ism. The first is that it is less efficient th<strong>an</strong> directly sending a<br />

message to <strong>an</strong> object. T he second is that it c<strong>an</strong> be more difficult to maintain as it is not immediately<br />

obvious what message is being sent to the object. Thirdly, it c<strong>an</strong> be <strong>an</strong> awful lot harder to debug code<br />

containing perform: messages. This is because the system back trace displ ayed in the debugger may<br />

not display the same chain of message sends that the user of the class expects.<br />

So where should you use it? The situations where you are likely to encounter it are almost all<br />

associated with the user interface. However, you should not try to avoid it just because it may be less<br />

efficient at run time <strong><strong>an</strong>d</strong> more difficult to debug. There may be situations in which the perform<br />

mech<strong>an</strong>ism c<strong>an</strong> allow the construction of very powerful <strong><strong>an</strong>d</strong> flexible frameworks. The import<strong>an</strong>t point<br />

to note is that in this situation there may be a trade off between flexibility <strong><strong>an</strong>d</strong> efficiency.<br />

25.3 The Dependency mech<strong>an</strong>ism<br />

There are a number of different relationships between objects in the <strong>Smalltalk</strong> environment which we<br />

have already looked at. In particular we have considered:<br />

206

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

Saved successfully!

Ooh no, something went wrong!