Identifying Responsibilities• Rule of thumb: Look for verbs in problem description • An example: Behavior of MessageQueue: – Add message to tail – Remove message from head – Test wheth
Trang 1The Object-Oriented Design Process
Trang 3From Problem to Code
• Three Phases:
– Analysis
– Design
– Implementation
Trang 4• Functional Specification is a part of requirements
specification It has the following characteristics:
– Completely defines tasks to be solved
– Free from internal contradictions
– Readable both by domain experts and software developers – Reviewable by diverse interested parties
Trang 5Design Phase
• Goals
– Identify classes
– Identify behavior of classes
– Identify relationships among classes
• Artifacts
– Textual description of classes and key methods
– Diagrams of class relationships
– Diagrams of important usage scenarios
– State diagrams for objects with rich state
Trang 6Implementation Phase
• Implementation is the realization of the software
design in a specific programming language
• Each unit (a single class) is implemented
separately
• Unit testing is done to ensure that each unit
functions properly with respect to its specification
before the units are integrated
• The individual units are integrated and tested as a whole to ensure that the entire software system
works properly conforming to its specification
Trang 7Case Study: Voice Mail System
• Program that simulates a telephone voice mail
system
• The system has a collection of mailboxes, each of which may be accessed by an extension number
• 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
• In GUI program, will use buttons for keys
Trang 8• Focus on concepts, not implementation
– MessageQueue stores messages
– Don't worry yet how the queue is implemented
Trang 9Categories of Classes
• Tangible Things
• Agents
• Events and Transactions
• Users and Roles
• Systems
• System interfaces and devices
• Foundational Classes
Trang 10Identifying Responsibilities
• Rule of thumb: Look for verbs in problem
description
• An example: Behavior of MessageQueue:
– Add message to tail
– Remove message from head
– Test whether queue is empty
• OO Principle: Every operation is the responsibility of
a single class
– Example: Add message to mailbox
Trang 11Identifying Class Relationships
• The following relationships can exist among classes:
– Dependency ("uses")
– Association , including aggregation and composition
– Generalization , including inheritance and implementation
• Among them, inheritance ("is-a"), aggregation
("has"), and dependency ("use") are the most basic
relationships
Trang 12Dependency Relationship
• Class A depends on class B:
– A method of A manipulates objects of B in the parameters, local variables, or return types
• Minimize dependency: reduce coupling
– Example: Removes dependence on System and Message
• UML notation for dependency
public class Message {
anywhere }
}
Replace with
MailBox MessageQueue
Trang 13• Association represents general binary relationships
between classes
• Association can have roles
• Implemented through instance fields
• An example and UML notation for association
relationship
Association
Trang 14• A special form of association
– Represents the has-a or part-whole relationship.
– Simply a structural relationship that distinguishes the
whole, that is, the aggregate class, from the parts, that is, the component class.
– Implemented through instance fields
• e.g MessageQueue aggregates Messages
• e.g Mailbox aggregates MessageQueue
• An example and UML notation for aggregation
MessageQueue Message
Trang 15Composition
• A stronger form of aggregation
– The aggregate class exclusively owns the component
class
– The lifetime of the components is entirely included in the lifetime of the aggregate
– Contained objects don't exist outside container
• An example and UML notation for composition:
message queues permanently contained in mail box
MailBox MessageQueue
Trang 16Association Direction
• Some associations are bidirectional
– Can navigate from either class to the other
– e.g Course has set of students, student has set of courses
• Some associations are directed
– Navigation is unidirectional
– e.g Message doesn't know about message queue
containing it
MessageQueue 0 * Message
Trang 17Multiplicities
• The number of instances one class relates to one
instance of another class.
– any number (0 or more): *
Trang 19Generalization - Inheritance relationship
• Inheritance relationship is formed when a class (or
an interface) extends another class (or interface)
• UML notation for generalization
Trang 20Generalization - implementation relation
• The implementation relation between a class and an interface
• Interface type describes a set of methods
No implementation, no state
• Class implements interface
if it implements its methods
• UML notation for
implementation relation
Comparable
<<interface>>
Message
Trang 21Use Cases
• Analysis technique
• Each use case focuses on a specific scenario
• Use case = sequence of actions
• Action = interaction between actor and computer
system
• Each action yields a result
• Each result has a value to one of the actors
• Use variations for exceptional situations
Trang 22Use-Case Model
Actors
Các Use Case
Trang 23Sample Use Case
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
Trang 24Sample Use Case Variations
• Variation #1
1.1 In step 3, user enters invalid extension number
1.2 Voice mail system speaks
You have typed an invalid mailbox number.
1.3 Continue with step 2.
Trang 25CRC Cards
• CRC = Classes, Responsibilities, Collaborators
– a very effective design tool in identifying classes, their
responsibilities, and relationships to other classes
• Developed by Beck and Cunningham
• Use an index card for each class
• Class name on top of card
• Responsibilities on left
• Collaborators on right
Trang 26CRC Cards
Trang 27CRC Cards
• Responsibilities should be high level without
concerning implementation details
• Between 1 - 3 responsibilities per card is ideal
• Collaborators are for the class, not for each
responsibility
Trang 28How do you create CRC models
A CRC model is created iteratively performing the
following steps:
• Find classes
• One card per class
• Walk through scenario of use cases, to find
responsibilities, define collaborators to fill in cards
Trang 29• Use case: "Leave a message"
– Caller connects to voice mail system
– Caller dials extension number
• "Someone" must locate mailbox
• Neither Mailbox nor Message can do this
• New class: MailSystem
• Responsibility: manage mailboxes
Trang 30Walkthroughs
Trang 31Other UML Diagrams
Many UML diagram types
We'll use three types:
• Class Diagrams
• Sequence Diagrams
• State Diagrams
Trang 32• Each diagram shows dynamics of scenario
• Object diagram: class name underlined
Sequence Diagrams
Trang 33Sequence Diagrams
Trang 34• Use for classes whose objects have interesting states
State Diagram
Trang 35Design Documentation
• Recommendation: Use Javadoc comments
• Leave methods blank
• Don't compile file, just run Javadoc
• Makes a good starting point for code later
/**
Adds a message to the end of the new messages.
@param aMessage a message
*/
public void addMessage(Message aMessage)
{
}
Trang 36Case Study: Voice Mail System
Trang 37Voice Mail System
• Program that simulates a telephone voice mail
system
• The system has a collection of mailboxes, each of which may be accessed by an extension number
• 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
• In GUI program, will use buttons for keys
Trang 38Use Case: Reach an Extension
1. User dials main number of 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
Trang 39Use Case: Leave a Message
1. Caller carries out Reach an Extension
2. Caller speaks message
3. Caller hangs up
4. System places message in mailbox
Trang 40Use Case: Log in
1. Mailbox owner carries out Reach an Extension
2. Mailbox owner types password and #
Default password = mailbox number To change, see Change the Passcode
3. System plays mailbox menu:
Enter 1 to retrieve your messages.
Enter 2 to change your passcode.
Enter 3 to change your greeting.
Trang 41Use Case: Retrieve Messages
1 Mailbox owner carries out Log in
2 Mailbox owner selects "retrieve messages" menu option
3 System plays message menu:
Press 1 to listen to the current message
Press 2 to delete the current message
Press 3 to save the current message
Press 4 to return to the mailbox menu
4 Mailbox owner selects "listen to current message"
5 System plays current new message, or, if no more new
System plays message menu
6 User selects "delete current message"
Message is removed
7 Continue with step 3
Trang 42Use Case: Retrieve Messages
Variation #1
1.1 Start at Step 6
1.2 User selects "save current message".
• Message is removed from new queue and appended to old queue
1.3 Continue with step 3.
Trang 43Use Case: Change the Greeting
1 Mailbox owner carries out Log in
2 Mailbox owner selects "change greeting" menu
option
3 Mailbox owner speaks new greeting
4 Mailbox owner presses #
5 System sets new greeting
Variation #1: Hang up before confirmation
1.1 Start at step 3.
1.2 Mailbox owner hangs up.
1.3 System keeps old greeting.
Trang 44Use Case: Change the Passcode
1 Mailbox owner carries out Log in
2 Mailbox owner selects "change passcode" menu
option
3 Mailbox owner dials new passcode
4 Mailbox owner presses #
5 System sets new passcode
Variation #1: Hang up before confirmation
1.1 Start at step 3.
1.2 Mailbox owner hangs up.
1.3 System keeps old passcode.
Trang 45CRC Cards for Voice Mail System
Some obvious classes
• Mailbox
• Message
• MailSystem
Trang 46Initial CRC Cards
Trang 47• Who interacts with user?
• Telephone takes button presses, voice input
• Telephone speaks output to user
Telephone
Trang 48• With whom does Telephone communicate
– With MailSystem?
• What if there are multiple telephones?
• Each connection can be in different state (dialing, recording, retrieving messages, )
• Should mail system keep track of all connection states?
• Better to give this responsibility to a new class
Connection
Trang 49Analyze Use Case: Leave a message
Connection
record it.
Trang 50Result of Use Case Analysis
Trang 51Result of Use Case Analysis
Trang 52Analyse Use Case: Retrieve messages
1 User types in passcode
Telephone notifies Connection
2 Connection asks Mailbox to check passcode.
– Add responsibility "manage passcode" to Mailbox
3 Connection sets current mailbox and asks Telephone to
speak menu
4 User selects "retrieve messages"
Telephone passes key to Connection
5 Connection asks Telephone to speak menu
6 User selects "listen to current message"
Trang 53Analyse Use Case: Retrieve messages
– Add "retrieve messages" to responsibility of Mailbox.
– Connection asks Telephone to speak message
9 User selects "save current message"
– Modify responsibility of Mailbox to "retrieve, save, delete
messages"
Trang 54Result of Use Case Analysis
Trang 55CRC Summary
• One card per class
• Responsibilities at high level
• Use scenario walkthroughs to fill in cards
• Usually, the first design isn't perfect
Trang 56UML Class Diagram for Mail System
• CRC collaborators yield dependencies
• Mailbox depends on MessageQueue
• Message doesn't depends on Mailbox
• Connection depends on Telephone,
MailSystem, Message, Mailbox
• Telephone depends on Connection
Trang 57Dependency Relationships
Trang 58Aggregation 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
• A connection has references to a mailsystem and a telephone
Trang 59UML Class Diagram for Voice Mail System
Trang 60Sequence Diagram for Use Case:
Leave a message
Trang 61Interpreting a Sequence Diagram
• Each key press results in separate call to dial, but only one is shown
• 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 call
Trang 62Sequence Diagram: Retrieve messages
Trang 63Connection State Diagram
Trang 64Final Class Diagram for Voice Mail System