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.

The one exception to this rule are subclasses of <strong>Object</strong>. This is because <strong>Object</strong> is the root class<br />

of all classes in <strong>Smalltalk</strong> <strong><strong>an</strong>d</strong> as you must create a new class by subclassing it from <strong>an</strong> existing class, it<br />

is typical to subclass off <strong>Object</strong> when there is no existing appropriate class.<br />

24.3.3 Capabilities of a subclass/class<br />

A subclass or class should accomplish one specific purpose, that is it should capture only one idea. If<br />

more th<strong>an</strong> one idea is encapsulated in a class you may be reducing the ch<strong>an</strong>ces for reuse as well as<br />

contravening the laws of encapsulation in object oriented systems. For example, you may have merged<br />

two concepts together so that one c<strong>an</strong> directly access the data of <strong>an</strong>other. This is rarely desirable.<br />

This guideline leads to small classes (in terms of methods, inst<strong>an</strong> ce variables <strong><strong>an</strong>d</strong> code). Breaking a<br />

class down costs little but may produce major gains in reusability <strong><strong>an</strong>d</strong> flexibility. If you find that when<br />

you try <strong><strong>an</strong>d</strong> separate one class into two or more classes, some of the code needs to be duplicated for<br />

each class, then the use of abstract classes c<strong>an</strong> also be very helpful. By placing the common code into a<br />

common abstract superclass, you may be able to avoid unnecessary duplication of that code.<br />

The following two guidelines are intended as <strong>an</strong> aid in identifying when a class should be split:<br />

1. Look in the class comment (if there is no class comment th<strong>an</strong> this is a bad sign in itself). Consider the<br />

following points:<br />

• Is the comment short <strong><strong>an</strong>d</strong> clear. If not is this a reflection on the class? If it is, then consider how<br />

the comment c<strong>an</strong> be broken down into a series of short clear comments. Now base the new<br />

classes around these comments.<br />

• If the comment is short <strong><strong>an</strong>d</strong> clear, do the class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce variables make sense within the<br />

context of the comment? If they do not, then the class needs to re -evaluated. It may be that the<br />

comment is inappropriate, or the class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce variables inappropriate.<br />

• If the comment is short <strong><strong>an</strong>d</strong> clear, do the class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce methods make sense within the<br />

context of the comment? Again if they do not then appropriate action should be taken.<br />

2. Look at the inst<strong>an</strong>ce variable references (i.e. look at where the inst<strong>an</strong>ce variable access methods are<br />

used). How are the inst<strong>an</strong>ce variables being used? Is their use in line with the class comment?<br />

Again, if the <strong>an</strong>swer to this is no, then appropriate action should be taken.<br />

24.3.4 Ch<strong>an</strong>ges to system classes<br />

In general attempts should be made to avoid making ch<strong>an</strong>ges to system classes if at all possible. Such<br />

ch<strong>an</strong>ges c<strong>an</strong> produce surprising side effects as well a reduc e the portability of your code <strong><strong>an</strong>d</strong> others. In<br />

addition you may find it difficult to support the ch<strong>an</strong>ges you have made to the system classes in future<br />

releases of the system.<br />

However, there are certain circumst<strong>an</strong>ces in which it is necessary <strong><strong>an</strong>d</strong> useful to extend system<br />

classes. However, it is <strong>an</strong> extremely bad idea to modify <strong>an</strong> existing method of a system class. This is<br />

because if you are not absolutely sure about what you are doing you c<strong>an</strong> affect the behaviour of very<br />

m<strong>an</strong>y objects. If you really feel you have to modify <strong>an</strong> existing class’s method(s), then it is much better<br />

to subclass off the existing system class <strong><strong>an</strong>d</strong> modify the method(s) behaviour(s) there. Then if you do<br />

something which has un<strong>an</strong>ticipated effects, those effects are localized.<br />

If you are merely adding new methods to <strong>an</strong> existing system class, then it is a very good idea to<br />

place your additions in a separate category. This me<strong>an</strong>s that not only is it easier to find these additions,<br />

but that you c<strong>an</strong> file out the category to a different file so that w hen updates to the system are delivered,<br />

you c<strong>an</strong> easily patch in your extensions.<br />

24.3.5 Creating new collection classes<br />

When working with collections, there is always the question of whether to subclass a new collection<br />

class to hold your data (as we did for th e Queue <strong><strong>an</strong>d</strong> Stack classes) or whether to define a non<br />

collection class which will hold a collection within one of its inst<strong>an</strong>ce variables <strong><strong>an</strong>d</strong> then provide<br />

methods which will access that inst<strong>an</strong>ce variable. For example, let us assume that we wish to define a<br />

new class called account, which will hold information on deposits <strong><strong>an</strong>d</strong> withdrawals. We believe that we<br />

198

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

Saved successfully!

Ooh no, something went wrong!