Object-Oriented Analysis and Design, Part 1
Object-Oriented Analysis and Design, Part 1
html
Introduction
This two-article series presents a problem I use both to teach and test
OO design. It is a simple but rich problem, strong on ''design,''
minimizing language, tool, and even inheritance concerns. The problem
represents a realistic work situation, where circumstances change
regularly. It provides a good touch point for discussions of even fairly
subtle designs in even very large systems. This problem was
successfully solved by six first-year computer science students with no
OO background. It was less successfully addressed by business people,
commercial programmers learning OO, and experienced OO
programmers.
1 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
To learn what design is, I have been asking my students for years now,
before we start on any OO lectures, to sketch a simple design of an
everyday system and present it to the class on two overhead
transparencies. No talking is to accompany the transparencies. The
class then tries to understand, from the transparencies, the design of the
system. At the end the class discusses which of the teams' presentations
best captured the notion of design.
The system I ask them to design, and am now asking you to design, is a
small, one-branch bank. Sketch the design for the whole thing, not just
the computer part. Take no more than 15 minutes. Work with someone
else, if possible. Capture the design on a single sheet of paper. (Your
pencil lets you put much more on a page than the fat transparency pens
I give the students.) You should be able to show this piece of paper to
pretty much anyone on the street, and they would recognize the bank's
basic design. On your marks, get set. Go.
2 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
I gave you the bank exercise just to introduce the four key items
3 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
needed for good design. The problem I use for instruction in this article
is the design of a coffee machine. In discussing and comparing designs
in this article, I shall focus on Component Names, Component Main
Responsibilities, and Intercomponent Communication Channels. The
services list will come to you through the requirements statement.
Expect me to ask, "Which component knows the price of the coffee?
Which object knows how to make the drink?" I shall show, and ask you
to show, a few interaction diagrams to demonstrate that the system
really works. Although you may use inheritance in solving the problem,
I am going to spend less time on inheritance. If you get the basic design
wrong, no inheritance structure will fix it. If your basic design is
strong, you can play with the inheritance structure to make it better.
Figure 1 shows a sample solution to the bank design question that uses
the four elements. Note that the Customer is not a component of the
bank and has no responsibilities. (There is no way, at system design
time, to distinguish a customer from a robber; that has to show up in
the operation of the system.) The components of the bank are:
The table. It provides a flat surface for writing and filling out
forms.
You and I are contractors who just won a bid to design a custom
4 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
coffee vending machine for the employees of Acme Fijet Works to use.
Arnold, the owner of Acme Fijet Works, like the common software
designer, eschews standard solutions. He wants his own, custom
design. He is, however, a cheapskate. Arnold tells us he wants a
simple machine. All he wants is a machine that serves coffee for 35
cents, with or without sugar and creamer. That's all. He expects us to
be able to put this little machine together quickly and for little cost.
We get together and decide there will be a coin slot and coin return,
coin return button, and four other buttons: black, white, black with
sugar, and white with sugar.
Kim puts in two quarters, then walks away from the machine and
forgets to come back.
5 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
Kim buys two coffees, white with sugar. The sugar dispenser
runs out of sugar after the first.
Figure 2 shows the basic design that students commonly give me at this
point, after we have straightened out the standard bugs. Figure 3 shows
the interaction diagram. The code in Listing 1 shows sample C++
declarations for the classes involved (a full implementation is delayed
for a better design). For space reasons, I omit details of making change
and handling insufficient change. Note this is not a good design; it is a
typical one. I hope you do better. Try to say why your design is
"better" than this one. The answer will be clear by the time we get
done.
Cash Box. Knows amount of money put in; gives change; knows
price of coffee; turns Front Panel on and off.
Arnold Visits
After five machines are installed and have been operating for a while,
Arnold comes along and says, "I would like to add bouillon, at
twenty-five cents. Change the design." We add one more button for
bouillon, and one more container for bouillon powder. How else do
you change your design?
6 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
7 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
Although I voice the worry, I cannot quantify it, and so we leave the
design the way it is, with either the front panel or the controller being
the mainframe object.
Arnold comes back a while later with a brilliant idea. He has heard
that some companies use their company badges to directly debit the
8 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
So now, the cash box answers only one question from the front panel:
does the customer have <amount> of credit? We become indifferent to
whether the customer pays by cash or direct debit. Further, payroll can
shut the credit down at a certain point for whatever reason. If your
original design already worked this way, give yourself ten extra credit
points. Even though it would have cost nothing to have designed this
way from the start, the design was not obvious at the start. You might
have arrived at the design if you had enquired after how the design
requirements might evolve in the future. The responsibility, "knows
how much money is put in," is sensitive to assumptions and to a
technology. "Knows whether there is sufficient credit" works the same
for cash, but is less specific about assumptions and technology. It is
therefore more robust. Design (3) (Figure 6) changes very little from
design (2). The new responsibilities are:
9 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
Front Panel. Same as before, but only asks Cash Box if sufficient
credit is available.
The code in Listing 2 implements Design 3. Try to say why this design
is or is not "better" - in arguable terms. Avoid arguments like, "because
one should/shouldn't use objects." I have even found that "simpler" is
an argument fraught with conflict. See what you come up with.
10 of 11 1/25/2001 6:30 PM
Feature Article: May98 http://www.cuj.com/archive/1605/feature.html
| Top | Search
11 of 11 1/25/2001 6:30 PM