Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
• provide a more me<strong>an</strong>ingful inst<strong>an</strong>ce creation interface (for example, Line from: (3 @ 3) to: (5 @<br />
5))<br />
• ensure that certain information is provided which is necessary to inst<strong>an</strong>tiate the class (for<br />
example Account newBal<strong>an</strong>ce: 24.00)<br />
• force <strong>an</strong> initialization routine to be executed before <strong>an</strong>y user of the object c<strong>an</strong> send it <strong>an</strong>y other<br />
messages.<br />
We shall consider the correct way to achieve the last of the above points. That is, we wish to force<br />
the execution of <strong>an</strong> initialization method before returning the newly created inst<strong>an</strong>ce. To do this we c<strong>an</strong><br />
redefine the message new in the inst<strong>an</strong>ce creation class side protocol of a class as follows:<br />
new<br />
^super new initialize<br />
This method says, use the inherited method new to create a new inst<strong>an</strong>ce of the class <strong><strong>an</strong>d</strong><br />
immediately send that new inst<strong>an</strong>ce the inst<strong>an</strong>ce message i nitialize (this is extremely good <strong>Smalltalk</strong><br />
style). In turn, the initialize method (defined on the class side) should have a format which follows this<br />
pattern:<br />
initialize<br />
super initialize "to initialize inherited inst<strong>an</strong>ce variables"<br />
"initialize variables that I define"<br />
24.4.5 Programming in terms of objects<br />
It is all too easy when you are first starting with <strong>Smalltalk</strong> to write procedure oriented code within<br />
methods. Indeed, in version 1.0 of VisualWorks there was code which had clearly been written by a C<br />
or C++ programmer rather th<strong>an</strong> a <strong>Smalltalk</strong> developer. In such situations the developer has been<br />
thinking in too procedurally a m<strong>an</strong>ner. In <strong>Smalltalk</strong> what the programmer should try to do is to think in<br />
terms of objects.<br />
24.4.6 Example methods<br />
Example methods are class side methods which illustrate how the inst<strong>an</strong>ces of <strong>an</strong> object should be used.<br />
They are particularly useful for complex classes which form part of a framework. In these classes it is<br />
often difficult to comprehend how the various classes work together, with out seeing them work<br />
together. The example methods c<strong>an</strong> then be filed out in a production system as they are not required for<br />
the correct functioning of the class.<br />
24.4.7 Test methods<br />
These have already been mentioned above <strong><strong>an</strong>d</strong> were discussed at length in the last few chapters.<br />
However, they are <strong>an</strong> extremely useful tool for the developer <strong><strong>an</strong>d</strong> should be encouraged at every<br />
opportunity.<br />
24.5 Naming message protocols<br />
An import<strong>an</strong>t style consideration for <strong>an</strong>y VisualWorks developer is the naming of protocols (or message<br />
categories). It is a good idea to follow the conventions laid down in the existing classes for two reasons:<br />
1. your own classes will follow the same pattern as those which have already been encountered <strong><strong>an</strong>d</strong><br />
will thus be easier to follow for you <strong><strong>an</strong>d</strong> others<br />
2. tools such as the Full Browser will allow you to see methods, defined in other classes higher up<br />
the class hierarchy, in a given category. When you w<strong>an</strong>t to name a new category you c<strong>an</strong> search<br />
for similar methods, when you find them you c<strong>an</strong> merely use the same category name.<br />
202