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.

24.2.4 Adding variables to system classes<br />

Always add class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce variables to the end of <strong>an</strong>y exist ing variables in a system class. This is<br />

because the interpreter on some <strong>Smalltalk</strong> implementations accesses the variables of certain classes by<br />

position rather th<strong>an</strong> by name!<br />

24.2.5 Accessing inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> class variables<br />

In general it is always better to access in st<strong>an</strong>ce <strong><strong>an</strong>d</strong> class variables via intermediate methods, referred to<br />

as accessor methods, rather th<strong>an</strong> access or set them directly. This is called variable-free programming.<br />

This promotes the modularity of your methods <strong><strong>an</strong>d</strong> insulates the methods against ch<strong>an</strong>ges in the way the<br />

object (or class) holds inst<strong>an</strong>ce (or class) information. This is a very import<strong>an</strong>t concept, as direct access<br />

to inst<strong>an</strong>ce variables c<strong>an</strong> limit the power of subclassing.<br />

It is also possible to protect the inst<strong>an</strong>ce (or class) variables from undes ired ch<strong>an</strong>ges. For example,<br />

you c<strong>an</strong> put preconditions on <strong>an</strong> access method, or return a copy of the contents of the variable so that it<br />

c<strong>an</strong>not be directly affected.<br />

24.3 Classes<br />

24.3.1 Naming classes<br />

The naming of a class is extremely import<strong>an</strong>t. The class is the core el<br />

program. Class names are always capitalized. For example:<br />

ement in <strong>an</strong>y object oriented<br />

• Collection<br />

• GraphicalClassBrowser<br />

• EmployeePensionPaymentsHistory<br />

The second <strong><strong>an</strong>d</strong> third class names above provide extremely good examples of how a class name c<strong>an</strong><br />

describe the class. This is because the name of the class is used by most developers to indicate its<br />

purpose or intent. This is partly due to the fact that it is the class name which will be used when<br />

searching for appropriate classes (for example by using the find class... menu option).<br />

The class name should therefore be descriptive, for example, classes with names such as MyClass or<br />

ProjectClass1 are of little use. However, class names should not be so specific that they make it appear<br />

that the class is unlikely to be of use except in one specific situation (unless of course this is the case).<br />

For example, in <strong>an</strong> application to record details about university lecturers, a class with a name such as<br />

ComputerScienceDepartmentLecturer is probably not appropriate unless it really does relate<br />

only to lecturers in the computer science department. If this is the case, you need to ask yourself in what<br />

way are computer science lecturers special over other lecturers.<br />

24.3.2 The role of subclasses<br />

A subclass should only be used to modify the behaviour of its parent class. This modification should be<br />

a refinement of the class; this c<strong>an</strong> be accomplished in three ways (or in combination):<br />

• Ch<strong>an</strong>ges to the external protocol, that is the set of messages that the inst<strong>an</strong>ces o f the class respond<br />

to.<br />

• Ch<strong>an</strong>ges in the implementation of the methods, that is ch<strong>an</strong>ges in the way the messages are<br />

h<strong><strong>an</strong>d</strong>led.<br />

• Additional behaviour, which references inherited behaviour.<br />

If a subclass does not provide one or more of the above, then it is incorrectly placed. For example, if<br />

a subclass implements a set of new methods, but no reference is made to the parent classes’ inst<strong>an</strong>ce<br />

variables or methods, then this class is not really a subclass of the parent (it doesn’t extend it).<br />

197

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

Saved successfully!

Ooh no, something went wrong!