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.
should use a dictionary to hold the actual data, but should Account be a subclass of Dictionary or<br />
a subclass of something else (for example <strong>Object</strong>)? The two options are illustrated below:<br />
<strong>Object</strong><br />
Account has in <strong>an</strong> inst<strong>an</strong>ce variable holding <strong>an</strong> inst<strong>an</strong>ce of class Dictionary<br />
or would it be better to have:<br />
<strong>Object</strong><br />
Dictionary<br />
Account<br />
Of course this point also depends on what you are going to do with the Account class. If it is<br />
providing a new collection class (in some way) even if it is only for your application, then the above<br />
should be born in mind. However, if what you are actually doing, is providing a functionally complex<br />
class which just happens to contain a dictionary, then the above would be the wrong way to do it. In this<br />
case it is almost certainly better to make the new class a subclass of <strong>Object</strong>.<br />
There are two further points to bear in mind about this. The first is tha t if you define <strong>an</strong>y inst<strong>an</strong>ce<br />
variables in Account, then when you examine <strong>an</strong>y inst<strong>an</strong>ce of Account you will find that you get the<br />
Dictionary inspector which does not display the inst<strong>an</strong>ce variables. This c<strong>an</strong> be quite infuriating. Of<br />
course you c<strong>an</strong> subclass th e inspector <strong><strong>an</strong>d</strong> extend it so that it does display the inst<strong>an</strong>ce variables. A<br />
second point is that if <strong>an</strong> inst<strong>an</strong>ce of Account needs to grow, then the contents of the inst<strong>an</strong>ce<br />
variables defined in Account will apparently disappear, unless you extend the copyEmpty method it<br />
will have inherited. This method must be extended such that the contents of the inst<strong>an</strong>ce variables is<br />
copied. Again, this is only a minor point, but c<strong>an</strong> be extremely <strong>an</strong>noying when you have spent a great<br />
deal of time attempting to track down a bizarre bug, only to find this is the root cause of your problem.<br />
24.3.6 Class comments<br />
Every class should have a class comment, no matter whether it is <strong>an</strong> abstract or concrete class. This<br />
comment should act as the basic documentation for the class. It should th erefore tell both a developer<br />
subclassing from the class, or a user of the class, what they need to know. The comment may also<br />
contain information about the author’s name, affiliation, history of the class’s modifications, as well as<br />
its purpose <strong><strong>an</strong>d</strong> status . Information which might be placed in the class comment includes the name of<br />
the class author, its purpose, who has modified the class, when <strong><strong>an</strong>d</strong> why, the type of class, inst<strong>an</strong>ce <strong><strong>an</strong>d</strong><br />
class variables (including the class of object they will hold <strong><strong>an</strong>d</strong> their use), collaborations between this<br />
class <strong><strong>an</strong>d</strong> others, example usage, copyright information <strong><strong>an</strong>d</strong> class specific information such as what a<br />
subclass of <strong>an</strong> abstract class is expected to redefine.<br />
24.3.7 A class or <strong>an</strong> inst<strong>an</strong>ce<br />
In some situations it may only ever be nec essary to create a single inst<strong>an</strong>ce of a class. This single<br />
inst<strong>an</strong>ce must be created <strong><strong>an</strong>d</strong> then referenced where it is required. A point of continued debate is<br />
whether it is worth creating such <strong>an</strong> inst<strong>an</strong>ce or whether it is better to define the required behavi our in<br />
class methods <strong><strong>an</strong>d</strong> reference the class (after all a class is <strong>an</strong> object as well). Invariably the <strong>an</strong>swer to this<br />
is no, for the following reasons:<br />
• Such <strong>an</strong> approach breaks the st<strong><strong>an</strong>d</strong>ards which have been set, not only in the VisualWorks image,<br />
but by the majority of <strong>Smalltalk</strong> developers. You will therefore be reducing the readability <strong><strong>an</strong>d</strong><br />
comprehensibility of your system.<br />
• The creation of <strong>an</strong> inst<strong>an</strong>ce requires a minimum of overhead. After all this is a key feature in<br />
<strong>Smalltalk</strong> <strong><strong>an</strong>d</strong> it has therefore received extensive attention.<br />
• You may require more th<strong>an</strong> one inst<strong>an</strong>ce sometime in the future. If you have implemented all the<br />
code on the class side, it will be necessary to move these methods onto the inst<strong>an</strong>ce side of the<br />
class.<br />
• In doing so, you may be tempted to tr eat the class as a global reference. This suggests that the<br />
implementation has been poorly thought out.<br />
199