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.

25. The Perform <strong><strong>an</strong>d</strong> Dependency Mech<strong>an</strong>isms<br />

25.1 <strong>Introduction</strong><br />

This chapter introduces the use of the perform mech<strong>an</strong>ism which provides <strong>an</strong> alternative method for<br />

sending messages. It then presents a practical discussion of the dependency mech<strong>an</strong>ism. This includes<br />

what the dependency mech<strong>an</strong>ism is, why you might w <strong>an</strong>t to use it, how to construct a dependency <strong><strong>an</strong>d</strong><br />

the effect that it has.<br />

25.2 The Perform mech<strong>an</strong>ism<br />

The perform mech<strong>an</strong>ism is a very import<strong>an</strong>t feature of <strong>Smalltalk</strong>. Some people consider it to be the<br />

most powerful feature available to the develope r while others consider it essential for the construction<br />

of truly reusable classes (it should be noted however th<strong>an</strong> m<strong>an</strong>y others consider it a very bad feature!).<br />

Those of you who have been exposed to l<strong>an</strong>guages such as Common LISP or POP11 will not find th is<br />

concept too str<strong>an</strong>ge, however those of you who have been brought up on l<strong>an</strong>guages such as Pascal <strong><strong>an</strong>d</strong><br />

ADA may find the perform mech<strong>an</strong>ism horrifying at first gl<strong>an</strong>ce. As with most l<strong>an</strong>guage constructs it<br />

c<strong>an</strong> be very useful if used judiciously.<br />

Normally, when a message is to be sent to <strong>an</strong> object, it is specified by the programmer when they<br />

write the method. For example:<br />

myMethod: <strong>an</strong><strong>Object</strong><br />

Tr<strong>an</strong>script show: <strong>an</strong><strong>Object</strong> printString.<br />

In this example, I have specified that first the object contained in the temporary variable <strong>an</strong><strong>Object</strong><br />

will be sent the message printString <strong><strong>an</strong>d</strong> that the result of this message expression will be used as a<br />

parameter with the message show: to the object Tr<strong>an</strong>script. This is all perfectly normal.<br />

However, in some cases the programmer may not know what messages the object passed to them<br />

may respond to, or they may not know what type of message should be sent. This may well be the case<br />

with a generalist class which is intended to act as some sort of framework which will be reused <strong><strong>an</strong>d</strong><br />

customized by the users of the class.<br />

One way around this problem would be to specify that the user of the class should write their own<br />

method (e.g. by using a subclass responsibility message) or should modify the current method<br />

specifying what the message should actually be. This second approach, (which would be the case, if we<br />

know what the method should do, but were lacking the name of one particular message to send to one<br />

particular object), is not really desirable.<br />

In some cases, even the person reusing the cl ass may not know what the message to send is; it may<br />

only be possible to determine that at run time. To get around this, a programmer might have to write a<br />

large case -style statement, which tested the object to see what type it was, <strong><strong>an</strong>d</strong> then selected the<br />

appropriate message to send depending on that type. Again, this is not really desirable as the number of<br />

possibilities may be large <strong><strong>an</strong>d</strong> each time a new message was used, the programmer would have to<br />

update the method accordingly.<br />

<strong>Smalltalk</strong> provides a way ar ound this which is referred to as the perform mech<strong>an</strong>ism. The<br />

perform: message (<strong><strong>an</strong>d</strong> those associated with it) c<strong>an</strong> be used to request that <strong>an</strong> object receive a<br />

message which is contained within a variable. For example:<br />

<strong>an</strong><strong>Object</strong> perform: someMessage.<br />

In this example, the object <strong>an</strong><strong>Object</strong> is specified to receive a message perform: with a single<br />

parameter. This parameter will be the contents of the variable someMessage. Thus at compile time,<br />

205

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

Saved successfully!

Ooh no, something went wrong!