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.

• A method should only send messages to a limited set of objects. This promotes<br />

maintainability <strong><strong>an</strong>d</strong> comprehensibility <strong><strong>an</strong>d</strong> increases the modularity of the method.<br />

• Have you used accessor methods for inst<strong>an</strong>ce variable access? This is known as variable-free<br />

programming. This c<strong>an</strong> greatly insulate the method from ch<strong>an</strong>ges in how the object holds<br />

information.<br />

• Is t he behaviour encapsulated by the method intended for public or private (to the object)<br />

use? If it is a mixture of the two, then the method should be decomposed into two or more<br />

methods. The private code should be placed in a method which is defined within a private<br />

protocol (see below). This indicates to developers that the method is not intended for external<br />

use.<br />

• Does the method rely more on the behaviour of other objects th<strong>an</strong> on its own object (that is,<br />

a series of messages is being sent to some object(s ) other th<strong>an</strong> “self”)? If so, the method may<br />

be better placed in <strong>an</strong>other object (or objects).<br />

This last point is worth considering in slightly more detail. The series of messages in such a method<br />

may be better placed in a method in the class of the receive r object. This is because it is really<br />

describing behaviour associated with that object. By placing it with the receiver object’s class, all<br />

modifications to the behaviour of the receiver are encapsulated in that object. In addition, this behaviour<br />

may be useful to other objects, by encoding it within the receiver’s class, they c<strong>an</strong> all gain access to that<br />

behaviour (rather th<strong>an</strong> having to duplicate it in a number of places). It is not easy to get good method<br />

level code reuse with poorly placed code. To do th is most messages should be sent to self. Note: This is<br />

probably one of the hardest things to do well in object oriented programming. However, if done<br />

correctly it c<strong>an</strong> pay very high dividends.<br />

24.4.3 Class methods versus inst<strong>an</strong>ce methods<br />

It may at first seem unclear what should normally go in a class (side) method as opposed to what should<br />

go in <strong>an</strong> inst<strong>an</strong>ce (side) method when defining a new class. After all they are both defined in the class!<br />

However, it is import<strong>an</strong>t to remember that one defines the behaviour of th e inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> the other the<br />

behaviour of the class (the class side methods). The class side methods should only perform one of the<br />

following roles:<br />

Inst<strong>an</strong>ce creation. This role is very import<strong>an</strong>t as it is often used to initialize inst<strong>an</strong>ce variables of <strong>an</strong><br />

object to appropriate values. For example, it is quite common to see a class side method called new<br />

used to send the message initialize to a new inst<strong>an</strong>ce.<br />

Class variable access. See below for a discussion of this role.<br />

Inquiries about the class. Such methods <strong>an</strong>swer with generally useful objects, frequently derived from<br />

class variables. For example, they may return the number of inst<strong>an</strong>ces of this object created.<br />

Inst<strong>an</strong>ce m<strong>an</strong>agement. To control the number of inst<strong>an</strong>ces created. For example, some classes only<br />

allow one single inst<strong>an</strong>ce of that class to be created.<br />

A documentation role. Class methods solely for documentation are sometimes used. Generally the<br />

class comment is a better place for this information unless such a facility is not available e.g. in<br />

<strong>Smalltalk</strong> V.<br />

Examples. Occasionally class methods are used to provide helpful examples which are aimed at<br />

explaining the operation of a class. This is good practice.<br />

Testing. Class side methods c<strong>an</strong> be used to support testing of <strong>an</strong> inst<strong>an</strong>ce of a class. Such meth ods c<strong>an</strong><br />

be used to create <strong>an</strong> inst<strong>an</strong>ce, perform some operation <strong><strong>an</strong>d</strong> then compare the result with a known<br />

value. If the values are different <strong>an</strong> error c<strong>an</strong> be reported. This is a very useful way of providing<br />

regression tests.<br />

Support for one of the above.<br />

Anything else should be performed by <strong>an</strong> inst<strong>an</strong>ce method.<br />

24.4.4 Class inst<strong>an</strong>ce creation methods<br />

It is quite common to w<strong>an</strong>t to redefine the way in which a new inst<strong>an</strong>ce is created. This c<strong>an</strong> be because<br />

you wish to:<br />

201

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

Saved successfully!

Ooh no, something went wrong!