CSE 341 -- Object-Oriented Design
These are some rough notes on object-oriented design.
Given an application to be constructed or a system to be modelled:
- Is there an existing application that can be adapted? Or is there a
framework that can be used? If so, use them! In any case you will be
using existing system classes for the lower-level parts of the application
or simulation.
- Otherwise identify the objects involved. Exactly what is an object
will depend on the application or what aspect of the system is to be
modelled.
- Identify relations among the objects, in particular part-whole
relations.
- Identify classes of objects, and inheritance relations among these
classes. Are there abstract superclasses that can be defined to capture
common properties?
- Specify messages that the objects should understand, and begin
defining them.
The part-whole and inheritance relations are often confused by new
object-oriented programmers. Examples of each: we might have an abstract
class Vehicle
and a subclass Bus
(inheritance
relation). A Bus
has parts such as an engine (which might be
an instance of class Engine
), a body, an electrical system,
and so forth. Parts can in turn have subparts. Note that
Engine
isn't a subclass of Bus
however!
A common mistake by novice programmers is to over-use inheritance, and
create very deep hierarchies of subclasses. By all means use inheritance
when it is appropriate, but don't use it just for the sake of using it.
To start, first sketch out a general system design. After that, many of
these steps are best done interactively -- construct classes and some test
instances, and begin defining and testing methods online. Don't write the
whole system and then test -- design things so that you can test parts as
you go.
Include method stubs for
methods not yet defined. I often define a method stub using this pattern:
frobnicate: s
"frobnicate this object using s as a milepost"
self notYetImplemented
where we the following definition in class Object:
notYetImplemented
self error: 'this method hasn''t been implemented yet'
The advantage of doing this is that we have placeholders, and can also
browse all senders of notYetImplemented
to see what still
needs to be filled in.
Smalltalk is well-suited to exploratory programming -- programming
systems where there isn't a complete specification yet. You'll need to
rearrange the class hierarchy, change object representation, etc. as you go.
Object-Oriented Design Methodologies
There are many of these: Booch, Rumbaugh, etc etc.
Often, the methodologies
include checklists of things to do, diagramming methods (e.g. UML) etc.
See for example
Author: Booch, Grady
Title: Object-Oriented Design with Applications
Edition: 2nd ed.
Publisher: Benjamin-Cummings Publishing Company
Year: 1994
Pages: 589p.
Frameworks
Reusable collections of classes -- subclass from these and adapt them to
construct an application. Examples: the Model-View-Controller framework;
the file system.
These need to be built with care: what are the right superclasses? what
are the right method stubs that let you specialize behavior easily?
Generally, you have to build a couple of applications first to see what the
right generalizations are.
Patterns
Patterns in object-oriented programming are idiomatic and recurring
structures of objects. A well-known book is a catalog of 23 design
patterns: Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides,
Design Patterns: Elements of Reusable Object-Oriented Software,
Addison-Wesley, 1994. (An intellectual antecedent is the work in
architecture of Christopher Alexander, including The Timeless Way of
Building and A Pattern Language.)
Example patterns:
- proxy
- command (make an object that represents a command -- the object can
then understand messages such as
do
and undo
.
- observer (used in MVC)
- state (uses indirection to achieve different behavior for an object
depending on its state)
- prototype
- singleton
- decorator (wrapper)
See also
the patterns home page the University of Illinois, or the Smalltalk
Patterns page at Object Arts. Or do a google search on "smalltalk
patterns".