Design Process
Ch2
Chapter Topics
Part I
From Problem to Code
The Object and Class Concepts
Identifying Classes
Identifying Responsibilities
Relationships Between Classes
Part II
Use Cases
CRC Cards
UML Class Diagrams
Sequence Diagrams
State Diagrams
Using javadoc for Design Documentation
Part III
Case Study: A Voice Mail System
Problem
Programming tasks originate
from the desire to solve a
particular problem
Three Phases:
(1) Analysis
(2) Design
(3) Implementation
Example
A word-processing program
Define terms
E.g., fonts, footnotes, multiple columns,
Note
The analysis phase concerns itself with the
description of WHAT needs to be done, not HOW
it should be done.
The selection of specific algorithms, such as those that
insert page breaks or sort the index, will be handled in
the implementation phase
In practice
The above are goals, not steps.
Usually not possible to find ALL classes first, then
give a COMPLETE description of their responsibility,
then elaborate CORRECTLY on their relationship
The discovery process is iterative
The identification of one aspect of a class may force
changes in or lead to the discovery of others
Note
The designer must strive for a result in
which
the classes are crisply defined and class
relationships are of manageable complexity.
(3) Implementation
Phase
Goal
Classes and methods are coded, tested, and deployed
Class:
A class describes a collection of similar
objects
They support a common set of operations
They have a common set of possible states.
(1) State
State
An object can store information that is the result of its
prior operations
The collection of all information held by an object=the
objects state
The state may determine how the object behaves in the
future.
(2) Behavior
Behavior of an object
Defined by the operations that an object
supports
I.e., objects only permit certain
operations (not others)
E.g., a mailbox object can add a
message to its collection but not encrypt
it.
(3) Identity
Possible for two or more objects to
support the same operations and
have the same state,
Yet to be different from each other.
Reminder
Goals of Design Phases
Identify the classes
Identify the responsibilities of these
classes
Identify the relationships among these
classes
Identifying Classes
Step 1
Rule of thumb: Look for nouns in problem
description (or the functional specification)
Which phase produces the functional specification?
Mailbox
Message
User
Passcode
Extension
Menu
Many, but not necessarily all of them, are good choices
for classes
Identifying Classes
Class name
Singular noun:
e.g., Message, Mailbox
Adjective prefixed:
e.g., RectangularShape, BufferedReader
Identifying Classes
Step 2
Turn your attention to other classes that are
required to carry out necessary work,
after harvesting the classes that are obvious from the
specification.
Focus on concepts
The exact implementation of the queue is of no
interest in design phase.
Identifying Classes
Step 3
Look at different categories of classes for
more identification:
Identifying Classes
(i) Tangible Things
They are the easiest classes to discover as
they are visible in the problem domain.
Identifying Classes
(iii) Event and Transaction classes
They are useful to model records of activities
that describle what happened in the past or what needs to be
done later
Identifying Classes
(v) System classes
Their roles are typically
to perform initialization and shutdown
to start the flow of input into the system
Identifying
Responsibilities
Rule of thumb: Look for verbs in problem
description
Example: voice mail system
Messages are recorded, played, and deleted;
Users log in;
Passcodes are checked.
Identifying
Responsibilities
Class Relationships
(1) Dependency ("uses")
(2) Aggregation ("has")
(3) Inheritance ("is")
As shown, dependency is an
asymmetric relationship
Coupling
To minimize dependency: reduce
coupling
It is one important design goal
Why ???
Coupling
Example:
public class Message
{
void print() {System.out.println(text);}
. . .
}
Multiplicities
Informally described as the has-a relationship.
There may be a 1:1 or 1:0...n relationship:
E.g.,: A mailbox has one greeting (1:1)
public class Mailbox
{. . .
private Greeting myGreeting;
}
Example:
A forwarded message is a message.
ForwardedMessage inherits from Message
Greeting does not inherit from Message (Can't
store greetings in mailbox)
Summary
From problem to code
Three
Analysis
phases?
Design
Indentify
Three
goals?
classes
Their
responsibil
ities
Their
relationshi
ps
Implementat
ion
Nouns in the
specification, etc
Verbs in the
specification, etc
1. Dependency ("uses")
2. Aggregation ("has")
3. Inheritance ("is")
Part II
Use Cases
An Analysis technique
To describe how a system should work
(formally)
Use Cases
A System
Scenario 1
Use Case 1
Scenario 2
Use Case 2
.
.
.
.
.
.
Scenario N
Use Case N
A System
interaction
between the actors
and the system
Use Cases
Voicemail system
Many scenarios:
Leave a message, Retrieve message, etc
Variation #2
2.1. After step 4, caller hangs up instead of
speaking message
2.3. Voice mail system discards empty message
CRC Cards
CRC = Classes, Responsibilities,
Collaborators
An effective design technique for
discovering classes, responsibilities, and
relationships
Proposed by Kent Beck and Ward Cunningham,
A Laboratory for Teaching Object-Oriented
Thinking,
OOPSLA 89 Conference, Oct 1-6, 1989.
CRC Cards
A CRC card = an index card for a class:
Class name (top)
Responsibilities (left)
Collaborators (right):
Other classes that need to collaborate with so that it can fulfill its
responsibilities
CRC Cards
Discourage you from piling too much responsibility into a single class
Low-tech
So they can be used by groups of designers gathered around a table
CRC Cards
Responsibilities should be high level
DONT write individual methods
Walkthroughs
CRC cards are intuitive for walking
through use cases:
Use case: "Leave a message"
Caller connects to voice mail system
Caller dials extension number
Walkthroughs
CRC cards are intuitive for walking through use cases:
Example:
Leave a Message
1. Caller dials main number of voice
mail system
2. System speaks prompt
Enter mailbox number followed
by #
3. User types extension number
4. System speaks
You have reached mailbox xxxx.
Please leave a message now
5. Caller speaks message
6. Caller hangs up
7. System places message in
mailbox
Walkthroughs
CRC cards are intuitive for walking through use cases:
Example:
Leave a Message
1. Caller dials main number of voice
mail system
by #
Tips
Avoid mission creep
If a class acquires too many responsibilities, then
consider splitting it in two.
UML Diagrams
Graphical notations are very
good for conveying design
information
Easier to extract relationship
information by looking at a diagram
than by reading documentation.
Which phrase produces documentation?
UML Diagrams
Three well-known researchers (Booch,
Rumbaugh, Jacobson) got together to
unify their disparate notations and
develop UML
UML = Unified Modeling Language
Many diagram types
We'll use three types:
Class Diagrams
Sequence Diagrams
State Diagrams
UML Diagrams
Best-known commercial programs:
Rational Rose
http://www.ibm.com/software/rational/
Together
http://wwww.borland.com/together
UMLet
http://www.umlet.com/
ArgoUML
http://www.gentleware.com
Class Diagrams
Rectangle with class name
Optional compartments
Attributes
Methods
Class Diagrams
Class Relationships
Use
Has
Is
Multiplicities
Composition
Special form of aggregation
Contained objects don't exist outside container
Example: MessageQueue permanently contained
in Mailbox
Association
Some designers don't like
aggregation
Because they feel it is too
implementation-specific
Association
Some associations are bidirectional
Can navigate from either class to the other
Example: Course has set of students, student has set of
courses
Interface Types
Interface type describes a set of
methods
No implementation, no state
Class implements interface if it
implements its methods
In UML, use stereotype interface
Tips
Use UML to inform, not to impress
Don't draw a single monster diagram
Each diagram must have a specific
purpose
Omit inessential details
Sequence Diagrams
Class diagram are static they display the
relationships.
A sequence diagram shows dynamics of a
particular scenario.
Sequence diagram describe interactions between
objects.
Sequence Diagrams
The text inside an object rectangle has one of the
formats:
objectName:ClassName (full description)
objectName (class not specified)
:ClassName (object not specified)
Activation bar
Executing a method
Call a
method
Start an bar
at the end
of the call
arrow
The bar
ends when
the method
returns
Self call
Object Construction
Tips
Principal purpose of a sequence diagram
To show the objects that are involved in
carrying out a particular scenario and the order
of the method calls that are executed
State Diagram
Some objects have a discrete set of states
(that affect their behavior).
Example: a voice mail system:
Waiting state: the system is idle and waiting
new call
Connected state: when a caller first connects
to it.
Recording state: after the caller chooses an
targeted mailbox
State Diagram
Waiting
A caller connected to the system
Voice recorded
Connecte
d
Destination chosen
Recording
Design Documentation
Recommendation: Use Javadoc comments
Leave methods blank
/**
Adds a message to the end of the new messages.
@param aMessage a message
*/
public void addMessage(Message aMessage)
{
}
Part III
Simplified version
Use text for voice, phone keys, hangup
1 2 ... 0 # on a single line means key
H on a single line means "hang up"
All other inputs mean voice
Analysis Phase
1
2
3
4
to
to
to
to
CRC Cards
Walkthrough
UML
Design Phase
Telephone
Who interacts with user?
Telephone takes button presses,
voice input
Telephone speaks output to user
Telephone
Two responsibilities:
Input from user
Output to user
Connection
With whom does Telephone communicate
With MailSystem?
Superficially, that sounds like a good idea.
Connection
Walkthrough
Now we have some idea of the
components of the system.
It is time for a simple scenario
walkthrough.
CRC Summary
One card per class
Responsibilities at high level
Use scenario walkthroughs to fill in
cards
Usually, the first design isn't perfect.
(You just saw the author's third
design of the mail system)
UML
Now we use UML to help design.
Dependency
Relationships
Aggregation
Relationships
A mail system has mailboxes
A mailbox has two message queues
A message queue has some number
of messages
A connection has a current mailbox.
Interpreting a Sequence
Diagram
Each key press results in separate call to dial, but
only one is shown
Connection wants to get greeting to play
Each mailbox knows its greeting
Connection must find mailbox object:
Call findMailbox on MailSystem object
Parameters are not displayed (e.g. mailbox
number)
Return values are not displayed (e.g. found
mailbox)
Note that connection holds on to that mailbox
over multiple calls
Connection State
Diagram
Message.java
MessageQueue.java
Mailbox.java
Connection.java
MailSystem.java
Telephone.java
MailSystemTester.java
Implementation Phrase