Chapter 3
Ob ject-Oriented Design
A cursory explanation of ob ject-oriented programming tends to emphasize the
++
syntactic features of languages suchasC or Delphi, as opp osed to their older,
non ob ject-oriented versions, C or Pascal. Thus, an explanation usually turns
rather quickly to issues such as classes and inheritance, message passing, and
virtual and static metho ds. But such a description will miss the most imp ortant
p oint of ob ject-oriented programming, which has nothing to do with syntax.
Working in an ob ject-oriented language that is, one that supp orts inheri-
tance, message passing, and classes is neither a necessary nor sucient condi-
tion for doing ob ject-oriented programming. As we emphasized in Chapters 1
and 2, the most imp ortant asp ect of OOP is the creation of a universe of largely
autonomous interacting agents. But how do es one come up with such a system?
The answer is a design technique driven by the determination and delegation of
resp onsibilities. The technique describ ed in this chapter is termed responsibility-
1
driven design.
3.1 Resp onsibility Implies Noninterference
As anyone can attest who can rememb er b eing a child, or who has raised children,
resp onsibility is a sword that cuts b oth ways. When you make an ob ject b e
it a child or a software system resp onsible for sp eci c actions, you exp ect a
certain b ehavior, at least when the rules are observed. But just as imp ortant,
resp onsibility implies a degree of indep endence or noninterference. If you tell a
child that she is resp onsible for cleaning her ro om, you do not normally stand
1
The past few years have seen a p oli eration of ob ject-oriented design techniques. See
the section on further reading at the end of this chapter for p ointers to some of the
alternatives. I have selected Resp onsibility-driven design, develop ed by Reb ecca Wirfs-
bro ck [Wirfs-Bro ck 1989b , Wirfs-Bro ck 1990 ] b ecause it is one of the simplest, and it facilitates
the transition from design to programming. Also in this chapter I intro duce some of the nota-
tional techniques made p opular by the Uni ed Mo delling Language, or UML. However, space
do es not p ermit a complete intro duction to UML, nor is it necessary for an understanding of
subsequent material in the b o ok. 49
50 CHAPTER 3. OBJECT-ORIENTED DESIGN
over her and watch while that task is b eing p erformed{that is not the nature of
resp onsibility. Instead, you exp ect that, having issued a directive in the correct
fashion, the desired outcome will b e pro duced.
Similarly, in the owers example from Chapter 1, when Chris gave the request
to the Florist to deliver owers to Robin, it was not necessary to stop to think
ab out how the request would be serviced. The orist, having taken on the
resp onsibility for this service, is free to op erate without interference on the part
of the customer Chris.
The di erence b etween conventional programming and ob ject-oriented pro-
gramming is in many ways the di erence between actively sup ervising a child
while she p erforms a task, and delegating to the child resp onsibility for that
p erformance. Conventional programming pro ceeds largely by doing something
to something else{mo difying a record or up dating an array, for example. Thus,
one p ortion of co de in a software system is often intimately tied, by control and
data connections, to many other sections of the system. Such dep endencies can
come ab out through the use of global variables, through use of p ointer values, or
simply through inappropriate use of and dep endence on implementation details
of other p ortions of co de. A resp onsibility-driven design attempts to cut these
links, or at least make them as unobtrusive as p ossible.
This notion might at rst seem no more subtle than the concepts of infor-
mation hiding and mo dularity, which are imp ortant to programming even in
conventional languages. But resp onsibility-driven design elevates information
hiding from a technique to an art. This principle of information hiding b ecomes
vitally imp ortant when one moves from programming in the small to program-
ming in the large.
One of the ma jor b ene ts of ob ject-oriented programming o ccurs when soft-
ware subsystems are reused from one pro ject to the next. For example, a sim-
ulation manager such as the one we will develop in Chapter 7 mightwork for
b oth a simulation of balls on a billiards table and a simulation of sh in a sh
tank. This ability to reuse co de implies that the software can have almost no
domain-sp eci c comp onents; it must totally delegate resp onsibility for domain-
sp eci c b ehavior to application-sp eci c p ortions of the system. The ability to
create such reusable co de is not one that is easily learned{it requires exp erience,
careful examination of case studies paradigms, in the original sense of the word,
and use of a programming language in which such delegation is natural and easy
to express. In subsequentchapters, we will present several such examples.
3.2 Programming in the Small and in the Large
The di erence b etween the development of individual pro jects and of more siz-
able software systems is often describ ed as programming in the small versus
programming in the large.
Programming in the small characterizes pro jects with the following attributes:
Co de is develop ed by a single programmer, or p erhaps by a very small
3.3. WHY BEGIN WITH BEHAVIOR? 51
collection of programmers. A single individual can understand all asp ects
of a pro ject, from top to b ottom, b eginning to end.
The ma jor problem in the software development pro cess is the design and
development of algorithms for dealing with the problem at hand.
Programming in the large, on the other hand, characterizes software pro jects
with features such as the following:
The software system is develop ed by a large team, often consisting of p eople
with many di erent skills. There may b e graphic artists, design exp erts, as
well as programmers. Individuals involved in the sp eci cation or design of
the system may di er from those involved in the co ding of individual com-
p onents, who may di er as well from those involved in the integration of
various comp onents in the nal pro duct. No single individual can b e con-
sidered resp onsible for the entire pro ject, or even necessarily understands
all asp ects of the pro ject.
The ma jor problem in the software development pro cess is the management
of details and the communication of information b etween diverse p ortions
of the pro ject.
While the b eginning student will usually be acquainted with programming
in the small, asp ects of many ob ject-oriented languages are b est understo o d as
resp onses to the problems encountered while programming in the large. Thus,
some appreciation of the diculties involved in developing large systems is a
helpful prerequisite to understanding OOP.
3.3 Why Begin with Behavior?
Why b egin the design pro cess with an analysis of b ehavior? The simple answer
is that the b ehavior of a system is usually understo o d long b efore any other
asp ect.
Earlier software development metho dologies those p opular b efore the ad-
vent of ob ject-oriented techniques concentrated on ideas suchascharacterizing
the basic data structures or the overall structure of function calls, often within
the creation of a formal sp eci cation of the desired application. But structural
elements of the application can be identi ed only after a considerable amount
of problem analysis. Similarly, a formal sp eci cation often ended up as a do cu-
ment understo o d by neither programmer nor client. But behavior is something
that can b e describ ed almost from the moment an idea is conceived, and often
unlike a formal sp eci cation can b e describ ed in terms meaningful to b oth the
programmers and the client.
Resp onsibility-Driven Design RDD, develop ed by Reb ecca Wirfs-Bro ck, is
an ob ject-oriented design technique that is driven by an emphasis on b ehavior
at all levels of development. It is but one of many alternative ob ject-oriented
52 CHAPTER 3. OBJECT-ORIENTED DESIGN
' $
P
P
H
P
H
P
P
H
P
H
H H
H
H H
Welcome
H
H H
H
to the
H H
P
P
I IKH
P H H
H
P
H
H H
the
H
P
H
H
H
H
H P H
H
H
H
H
Interactive
P H
P
H
H
H
H
H
P
H
H
H
H
Intelligent H
H
P
H
H
H
H
H
P
H
H
H
H
H
Kitchen
P
H
H
H
H
H
H
H
H
H
H
H
Help er
H
H
H
H
H
H
H
H
H
H
H
H
Press Return
H
to b egin
H
H
H
H
H
H
&
22222222
222222222
2222222
Figure 3.1: { View of the InteractiveIntelligent Kitchen Help er.
design techniques. We will illustrate the application of Resp onsibility-Driven
Design with a case study.
3.4 A Case Study in RDD
Imagine you are the chief software architect in a ma jor computer rm. One day
your b oss walks into your oce with an idea that, it is hop ed, will b e the next
ma jor success in your pro duct line. Your assignmentistodevelop the Interactive
Intel ligent Kitchen Helper Figure 3.1.
The task given to your software team is stated in very few words written
on what app ears to b e the back of a slightly-used dinner napkin, in handwriting
that app ears to b e your b oss's.
3.4.1 The Interactive Intelligent Kitchen Help er
Brie y, the InteractiveIntelligent Kitchen Help er I IKH is a PC-based appli-
cation that will replace the index-card system of recip es found in the average
kitchen. But more than simply maintaining a database of recip es, the kitchen
help er assists in the planning of meals for an extended p erio d, saya week. The
3.4. A CASE STUDY IN RDD 53
user of the I IKH can sit down at a terminal, browse the database of recip es,
and interactively create a series of menus. The I IKH will automatically scale the
recip es to anynumb er of servings and will print out menus for the entire week,
for a particular day, or for a particular meal. And it will print an integrated
gro cery list of all the items needed for the recip es for the entire p erio d.
As is usually true with the initial descriptions of most software systems,
the sp eci cation for the I IKH is highly ambiguous on a number of imp ortant
p oints. It is also true that, in all likeliho o d, the eventual design and development
of the software system to supp ort the I IKH will require the e orts of several
programmers working together. Thus, the initial goal of the design team must
be to clarify the ambiguities in the description and to outline how the pro ject
can be divided into comp onents to be assigned for development to individual
team memb ers.
The fundamental cornerstone of ob ject-oriented programming is to charac-
terize software in terms of behavior; that is, actions to be p erformed. We will
see this rep eated on many levels in the development of the I IKH. Initially, the
team will try to characterize, at a very high level of abstraction, the b ehavior
of the entire application. This then leads to a description of the b ehavior of
various software subsystems. Only when all b ehavior has b een identi ed and
describ ed will the software design team pro ceed to the co ding step. In the next
several sections we will trace the tasks the software design team will p erform in
pro ducing this application.
3.4.2 Working through Scenarios
The rst task is to re ne the sp eci cation. As we have already noted, initial
sp eci cations are almost always ambiguous and unclear on anything except the
most general p oints. There are several goals for this step. One ob jective is
to get a b etter handle on the \lo ok and feel" of the eventual pro duct. This
information can then b e carried back to the client in this case, your b oss to see
if it is in agreement with the original conception. It is likely, p erhaps inevitable,
that the sp eci cations for the nal application will change during the creation of
the software system, and it is imp ortant that the design b e develop ed to easily
accommo date change and that p otential changes b e noted as early as p ossible.
Equally imp ortant, at this p ointvery high level decisions can b e made concerning
the structure of the eventual software system. In particular, the activities to b e
p erformed can b e mapp ed onto comp onents.
In order to uncover the fundamental b ehavior of the system, the design team
rst creates a number of scenarios. That is, the team acts out the running of
the application just as if it already p ossessed a working system. An example
scenario is shown in Figure 3.2.
54 CHAPTER 3. OBJECT-ORIENTED DESIGN
Simple Browsing
Alice Smith sits down at her computer and starts the I IKH. When the program
b egins, it displays a graphical image of a recip e box, and identi es itself as the
I IKH, pro duct of I IKH incorp orated. Alice presses the return button to b egin.
In resp onse to the key press, Alice is given a choice of a numb er of options. She
elects to browse the recip e index, lo oking for a recip e for Salmon that she wishes to
prepare for dinner the next day. She enters the keyword Salmon, and is shown in
resp onse a list of various recip es. She rememb ers seeing an interesting recip e that
used dill-weed as a avoring. She re nes the search, entering the words Salmon and
dill-weed. This narrows the searchtotwo recip es.
She selects the rst. This brings up a new window in which an attractive picture
of the nished dish is displayed, along with the list of ingredients, preparation steps,
and exp ected preparation time. After examining the recip e, Alice decides it is not
the recip e she had in mind. She returns to the search result page, and selects the
second alternative.
Examining this dish, Alice decides this is the one she had in mind. She requests
a printing of the recip e, and the output is sp o oled to her printer. Alice selects
\quit" from a program menu, and the application quits.
Figure 3.2: An Example Scenario
3.5. CRC CARDS{RECORDING RESPONSIBILITY 55
3.4.3 Identi cation of Comp onents
The engineering of a complex physical system, such as a building or an auto-
mobile engine, is simpli ed by dividing the design into smaller units. So, to o,
the engineering of software is simpli ed by the identi cation and development
of software comp onents. A component is simply an abstract entity that can
p erform tasks{that is, ful ll some resp onsibilities. At this p oint, it is not nec-
essary to know exactly the eventual representation for a comp onent or how a
comp onent will p erform a task. A comp onentmay ultimately b e turned into a
function, a structure or class, or a collection of other comp onents. At this level
of development there are just two imp ortantcharacteristics:
A comp onentmust have a small well-de ned set of resp onsibilities.
A comp onent should interact with other comp onents to the minimal extent
p ossible.
We will shortly discuss the reasoning b ehind the second characteristic. For the
momentwe are simply concerned with the identi cation of comp onent resp onsi-
bilities.
3.5 CRC Cards{Recording Resp onsibility
As the design team walks through the various scenarios they have created, they
identify the comp onents that will be p erforming certain tasks. Every activity
that must take place is identi ed and assigned to some comp onent as a resp on-
sibility.
Comp onent Name Collab orators
List of
Description of the
other components
resp onsibilities assigned
to this comp onent
56 CHAPTER 3. OBJECT-ORIENTED DESIGN
As part of this pro cess, it is often useful to represent comp onents using small
index cards. Written on the face of the card is the name of the software comp o-
nent, the resp onsibilities of the comp onent, and the names of other comp onents
with which the comp onentmust interact. Such cards are sometimes known as
CRC Comp onent, Resp onsibility, Collab orator cards, and are asso ciated with
each software comp onent. As resp onsibilities for the comp onent are discovered,
they are recorded on the face of the CRC card.
3.5.1 Give Comp onents a Physical Representation
While working through scenarios, it is useful to assign CRC cards to di erent
memb ers of the design team. The memb er holding the card representing a com-
p onent records the resp onsibilities of the asso ciated software comp onent, and
acts as the \surrogate" for the software during the scenario simulation. He or
she describ es the activities of the software system, passing \control" to another
memb er when the software system requires the services of another comp onent.
An advantage of CRC cards is that they are widely available, inexp ensive,
and erasable. This encourages exp erimentation, since alternative designs can b e
tried, explored, or abandoned with little investment. The physical separation of
the cards encourages an intuitive understanding of the imp ortance of the logical
separation of the various comp onents, helping to emphasize the cohesion and
coupling whichwe will describ e shortly. The constraints of an index card are
also a go o d measure of approximate complexity{a comp onent that is exp ected to
p erform more tasks than can t easily in this space is probably to o complex, and
the team should nd a simpler solution, p erhaps bymoving some resp onsibilities
elsewhere to divide a task b etween two or more new comp onents.
3.5.2 The What/Who Cycle
As we noted at the b eginning of this discussion, the identi cation of comp onents
takes place during the pro cess of imagining the execution of a working system.
Often this pro ceeds as a cycle of what/who questions. First, the design team
identi es what activity needs to b e p erformed next. This is immediately followed
by answering the question of who p erforms the action. In this manner, designing
a software system is much like organizing a collection of p eople, such as a club.
Any activity that is to b e p erformed must b e assigned as a resp onsibility to some
comp onent.
A p opular bump er sticker states that phenomena can and will sp ontaneously
o ccur. The bump er sticker uses a slightly shorter phrase. We know, however,
that in real life this is seldom true. If any action is to take place, there must b e
an agent assigned to p erform it. Just as in the running of a club any action to b e
p erformed must b e assigned to some individual, in organizing an ob ject-oriented
program all actions must b e the resp onsibility of some comp onent. The secret
to go o d ob ject-oriented design is to rst establish an agent for each action.
3.6. COMPONENTS AND BEHAVIOR 57
3.5.3 Do cumentation
At this p oint the development of do cumentation should b egin. Two do cuments
should b e essential parts of any software system: the user manual and the system
design do cumentation. Work on b oth of these can commence even b efore the
rst line of co de has b een written.
The user manual describ es the interaction with the system from the user's
p oint of view; it is an excellent means of verifying that the development team's
conception of the application matches the client's. Since the decisions made in
creating the scenarios will closely match the decisions the user will b e required to
make in the eventual application, the development of the user manual naturally
dovetails with the pro cess of walking through scenarios.
Before any actual co de has b een written, the mindset of the software team
is most similar to that of the eventual users. Thus, it is at this p oint that the
develop ers can most easily anticipate the sort of questions to which a novice
user will need answers. A user manual is also an excellent to ol to verify that
the programming team is lo oking at the problem in the same way that the client
intended. A client seldom presents the programming team with a complete and
formal sp eci cation, and thus some reassurance and two-way communication
early in the pro cess, b efore actual programming has b egun, can prevent ma jor
misunderstandings.
The second essential do cument is the design do cumentation. The design
do cumentation records the ma jor decisions made during software design, and
should thus be pro duced when these decisions are fresh in the minds of the
creators, and not after the fact when many of the relevant details will have b een
forgotten. It is often far easier to write a general global description of the software
system early in the development. To o so on, the fo cus will move to the level of
individual comp onents or mo dules. While it is also imp ortant to do cument the
mo dule level, to o much concern with the details of each mo dule will make it
dicult for subsequent software maintainers to form an initial picture of the
larger structure.
CRC cards are one asp ect of the design do cumentation, but many other
imp ortant decisions are not re ected in them. Arguments for and against any
ma jor design alternatives should b e recorded, as well as factors that in uenced
the nal decisions. A log or diary of the pro ject schedule should b e maintained.
Both the user manual and the design do cuments are re ned and evolveover time
in exactly the same way the software is re ned and evolves.
3.6 Comp onents and Behavior
To return to the I IKH, the team decides that when the system b egins, the user
will b e presented with an attractive informative window see Figure 3.1. The
resp onsibility for displaying this window is assigned to a comp onent called the
Greeter. In some as yet unsp eci ed manner p erhaps by pull-down menus, button
58 CHAPTER 3. OBJECT-ORIENTED DESIGN
Greeter Collab orators
Database Manager
Display Informative Initial Message
Plan Manager
O er User Choice of Options
Pass Control to either
Recip e Database Manager
Plan Manager for pro cessing
Figure 3.3: {CRC card for the Greeter.
or key presses, or use of a pressure-sensitive screen, the user can select one of
several actions. Initially, the team identi es just ve actions:
1. Casually browse the database of existing recip es, but without reference to
any particular meal plan.
2. Add a new recip e to the database.
3. Edit or annotate an existing recip e.
4. Review an existing plan for several meals.
5. Create a new plan of meals.
These activities seem to divide themselves naturally into two groups. The
rst three are asso ciated with the recip e database; the latter two are asso ciated
with menu plans. As a result, the team next decides to create comp onents
corresp onding to these two resp onsibilities. Continuing with the scenario, the
team elects to ignore the meal plan management for the moment and moveon
to re ne the activities of the Recip e Database comp onent. Figure 3.3 shows the
initial CRC card representation of the Greeter.
Broadly sp eaking, the resp onsibility of the recip e database comp onent is
simply to maintain a collection of recip es. We have already identi ed three
elements of this task: The recip e comp onent database must facilitate browsing
the library of existing recip es, editing the recip es, and including new recip es in the database.
3.6. COMPONENTS AND BEHAVIOR 59
3.6.1 Postp oning Decisions
There are a numb er of decisions that must eventually b e made concerning how
b est to let the user browse the database. For example, should the user rst
b e presented with a list of categories, such as \soups," \salads," \main meals,"
and \desserts"? Alternatively, should the user b e able to describ e keywords to
narrow a search, p erhaps by providing a list of ingredients, and then see all the
recip es that contain those items \Almonds, Strawb erries, Cheese", or a list
of previously inserted keywords \Bob's favorite cake"? Should scroll bars b e
used or simulated thumb holes in a virtual b o ok? These are fun to think ab out,
but the imp ortant p oint is that such decisions do not need to b e made at this
p oint see Section 3.6.2, \Preparing for Change". Since they a ect only a single
comp onent, and do not a ect the functioning of any other system, all that is
necessary to continue the scenario is to assert that by some means the user can
select a sp eci c recip e.
3.6.2 Preparing for Change
It has b een said that all that is constant in life is the inevitability of uncertainty
and change. The same is true of software. No matter how carefully one tries
to develop the initial sp eci cation and design of a software system, it is almost
certain that changes in the user's needs or requirements will, sometime during
the life of the system, force changes to b e made in the software. Programmers
and software designers need to anticipate this and plan accordingly.
The primary ob jective is that changes should a ect as few comp onents
as p ossible. Even ma jor changes in the app earance or functioning of an
application should b e p ossible with alterations to only one or two sections
of co de.
Try to predict the most likely sources of change and isolate the e ects
of such changes to as few software comp onents as p ossible. The most
likely sources of change are interfaces, communication formats, and output
formats.
Try to isolate and reduce the dep endency of software on hardware. For
example, the interface for recip e browsing in our application may dep end
in part on the hardware on which the system is running. Future releases
maybe p orted to di erent platforms. A go o d design will anticipate this
change.
Reducing coupling between software comp onents will reduce the dep en-
dence of one up on another, and increase the likeliho o d that one can be
changed with minimal e ect on the other.
In the design do cumentation maintain careful records of the design pro cess
and the discussions surrounding all ma jor decisions. It is almost certain
60 CHAPTER 3. OBJECT-ORIENTED DESIGN
that the individuals resp onsible for maintaining the software and designing
future releases will b e at least partially di erent from the team pro ducing
the initial release. The design do cumentation will allow future teams to
know the imp ortant factors b ehind a decision and help them avoid sp ending
time discussing issues that have already b een resolved.
3.6.3 Continuing the Scenario
Each recip e will b e identi ed with a sp eci c recip e comp onent. Once a recip e is
selected, control is passed to the asso ciated recip e ob ject. A recip e must contain
certain information. Basically,it consists of a list of ingredients and the steps
needed to transform the ingredients into the nal pro duct. In our scenario, the
recip e comp onentmust also p erform other activities. For example, it will display
the recip e interactively on the terminal screen. The user may b e given the ability
to annotate or change either the list of ingredients or the instruction p ortion.
Alternatively, the user may request a printed copy of the recip e. All of these
actions are the resp onsibility of the Recip e comp onent. For the moment, we will
continue to describ e the Recip e in singular form. During design we can think of
this as a prototypical recip e that stands in place of a multitude of actual recip es.
We will later return to a discussion of singular versus multiple comp onents.
Having outlined the actions that must take place to p ermit the user to browse
the database, we return to the recip e database manager and pretend the user
has indicated a desire to add a new recip e. The database manager somehow
decides in which category to place the new recip e again, the details of how this
is done are unimp ortant for our development at this p oint, requests the name of
the new recip e, and then creates a new recip e comp onent, p ermitting the user to
edit this new blank entry. Thus, the resp onsibilities of p erforming this new task
are a subset of those we already identi ed in p ermitting users to edit existing
recip es.
Having explored the browsing and creation of new recip es, we return to the
Greeter and investigate the development of daily menu plans, which is the Plan
Manager's task. In some way again, the details are unimp ortant here the
user can save existing plans. Thus, the Plan Manager can either be started
by retrieving an already develop ed plan or by creating a new plan. In the
latter case, the user is prompted for a list of dates for the plan. Each date is
asso ciated with a separate Date comp onent. The user can select a sp eci c date for
further investigation, in which case control is passed to the corresp onding Date
comp onent. Another activity of the Plan Manager is printing out the recip es for
the planning p erio d. Finally, the user can instruct the Plan Manager to pro duce
a gro cery list for the p erio d.
The Date comp onent maintains a collection of meals as well as any other an-
notations provided by the user birthday celebrations, anniversaries, reminders,
and so on. It prints information on the display concerning the sp eci ed date.
By some means again unsp eci ed, the user can indicate a desire to print all
the information concerning a sp eci c date or cho ose to explore in more detail a
3.6. COMPONENTS AND BEHAVIOR 61
Greeter
H
H