Upload
jasmin-lindsey
View
215
Download
1
Embed Size (px)
Citation preview
CS 151: Object-Oriented DesignSeptember 5 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2013Instructor: Ron Mak
www.cs.sjsu.edu/~mak
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
2
Class Relationships: Dependency
Class C depends on class D: Some method of C manipulates objects of D Example: Mailbox objects manipulate Message objects.
Dependency is asymmetric. The Message class is not aware of the existence of the
Mailbox class. Therefore, Message objects do not depend on Mailbox
objects.
Loose coupling Minimize the number of dependency relationships. Another way for a design to handle change.
_
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
3
Class Relationships: Aggregation
Class C aggregates class A: Objects of class C contains objects of class A
over a period of time.
A special case of dependency. The “has-a” relationship. Example: An Inventory object has a list of Instrument
objects.
Multiplicity 1:1 – Example: Each Person object has a single
StreetAddress object. 1:n – Example: Each Inventory object has an array of
multiple Instrument objects.
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
4
Class Relationships: Inheritance
Class C inherits from class S. The “is-a” relationship. All class C objects are special cases of class S objects. Class S is the superclass of class C. Class C is a subclass of class S. An object of class C is an object of class S.
Note Aggregation: A Mailbox object has a Message object. Inheritance: A ForwardedMessage object
is a Message object._
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
5
Class-Responsibilities-Collaborations
An effective technique to discover classes, responsibilities, and relationships.
CRC card is an index card that Describes one class. Lists its responsibilities. Lists its relationship with other classes.
_
The CRC Technique
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
6
Example: class Mailbox
The CRC Technique
Mailbox
Responsibilities Relationships
Manage passcode MessageQueue
Manage greeting
Create new message
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
7
1. Write the class name on each index card.
2. Distribute the responsibilities among the classes.
3. Find out their relationships and list all dependencies of each classes.
4. Don't write the methods or instance fields. Just write the responsibilities at a high level language.
_
The CRC Technique
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
8
A picture is worth a thousand words!
It is much easier to extract information from a graphical notation than reading a textual document.
Show your design in graphical UML diagrams. UML: Unified Modeling Language
There are several different types of UML diagrams. For now, we’ll use Class diagrams Sequence diagrams State diagrams
UML Diagrams
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
9
A class diagram has three compartments: A class diagram has three compartments:
UML Class Diagram
Class Name
Attributes : types
Methods(parms : types) : return type
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
10
Specify the most important attributes and methods. Specify the most important attributes and methods.
If you have too many attributes in a class, If you have too many attributes in a class, check if you can group them into a new class.check if you can group them into a new class.
Example: Example: You have attributes that are specific to your class.You have attributes that are specific to your class. But you also have name, street, city, state, and zip attributes.But you also have name, street, city, state, and zip attributes. Create a new Create a new AddressAddress class to contain those attributes. class to contain those attributes. Then your class Then your class has anhas an address. address.
UML Class Diagram: Attributes and Methods
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
11
Example UML Class Diagram
Mailbox
newMessages : ArrayList<Message>savedMessages : ArrayList<Message>
add(msg : Message) : booleangetCurrentMessage() : Message
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
12
Relationships among classes using arrows. Relationships among classes using arrows.
UML Class Diagram: Relationships
DependencyDependency
AggregationAggregation
InheritanceInheritance
CompositionComposition
AssociationAssociation
Direct associationDirect association
Interface implementationInterface implementation
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
13
Multiplicity in a “has” relationship. Multiplicity in a “has” relationship.
UML Class Diagram: Multiplicities
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
14
UML Class Diagram: Aggregation
Aggregation A “has a” relationship.A “has a” relationship. The contained object The contained object cancan have an existence have an existence
independent of its container.independent of its container.
ExampleExample A mailbox A mailbox has ahas a set of messages. set of messages. A message A message cancan exist without a mailbox. exist without a mailbox. Therefore, a mailbox Therefore, a mailbox aggregatesaggregates messages. messages.
Mailbox Message11 **
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
15
UML Class Diagram: Composition
Mailbox MessageQueue11 11
Composition A “has a” relationship.A “has a” relationship. The contained object The contained object cannotcannot (logically) have an existence (logically) have an existence
independent of its container.independent of its container.
ExampleExample A mailbox A mailbox has ahas a message queue. message queue. The message queue The message queue cannotcannot (logically) exist without a mailbox. (logically) exist without a mailbox. Therefore, a mailbox Therefore, a mailbox composescomposes a message queue. a message queue.
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
16
A class diagram is static. It shows the classes that exist
throughout the lifetime of the system.
A sequence diagram shows the dynamic relationships among the classes at run time. It describes interaction among objects over time during run time.
_
UML Sequence Diagram
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
17
Customer
Withdraw cash Keypad Bank AccountDisplay
selectnotify
display confirmation
enter amount
notifyverify
acceptnotify
display bank adsnotify
dispense cash
TIME
Withdraw Cash Sequence Diagram
UML Sequence Diagram
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
18
Use underlined font for object names to distinguish between class and object names.
The dashed vertical lines are lifelines. A rectangle on a lifeline is an activation bar.
It shows when a object has control executing a method.
The activation bar ends when the method returns. The horizontal arrows are call arrows. Use a sequence diagram to illustrate
complex interactions among a set of objects. Don't show loops or branches.
_
UML Sequence Diagram
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
19
At run time, the state of an object is characterizedAt run time, the state of an object is characterizedby the values of its fields.by the values of its fields.
In some objects, different states can cause In some objects, different states can cause different behaviors.different behaviors.
Example: A voice mail systemExample: A voice mail system Caller dials voice mail number Caller dials voice mail number the state is "Connected" the state is "Connected" If caller dial a valid extension If caller dial a valid extension the state is "Recording" the state is "Recording"
If in "Recording" state the caller can type in the passcode If in "Recording" state the caller can type in the passcode the state is "Main menu“ the state is "Main menu“__
UML State Diagram
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
20
Connected
Recording
Main menu
Changepasscode
Passcode Entered
Passcode Entered
Hang upHang up
# 3# 3
Passcode Entered
Passcode Entered
Extension Entered
Extension Entered
Hang upHang up
UML State Diagram
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
21
Free UML Design Tools
StarUML: StarUML: http://staruml.sourceforge.net/en/
Violet: Violet: http://horstmann.com/violet/ Dr. Horstmann’s productDr. Horstmann’s product
__
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
22
The The javadocjavadoc utility creates a set of utility creates a set of HTML pages for your classes. HTML pages for your classes.
It reads specially formatted comments in your code.It reads specially formatted comments in your code. Delimit comments for javadoc by Delimit comments for javadoc by /**/** and and */*/
It copies the first sentence of your comment to a It copies the first sentence of your comment to a summary table.summary table. Write the first sentence carefully.Write the first sentence carefully. It should start with an uppercase letter and end with a period.It should start with an uppercase letter and end with a period.
javadoc
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
23
javadoc
Javadoc comment example:
Method parameter: Method parameter: @param@param paramname explantion paramname explantion
Function return value: Function return value: @return@return explanation explanation
If the method throws an exception:If the method throws an exception:@throws@throws exceptionname explanation exceptionname explanation
/** * Choose the test string with the highest score and return it. * @param sequence the current sequence. * @param store the sequence store. * @return the chosen test string. */private String chooseString(CurrentSequence sequence, SequenceStore store){ ...}
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
24
On the command line: On the command line: javadocjavadoc pathpath/*.java/*.java
You can also run javadoc from within NetBeans or Eclipse._
Run the javadoc Utility
Demo
SJSU Dept. of Computer ScienceSpring 2013: September 3
CS 151: Object-Oriented Design© R. Mak
25
Write only the skeletons of your classes.Write only the skeletons of your classes. No implementation initially.No implementation initially. Just include javadoc comments.Just include javadoc comments. The result is an outline summary of your design.The result is an outline summary of your design.
Example:Example:
Program Design and javadoc
/** * Choose the test string with the highest score and return it. * @param sequence the current sequence. * @param store the sequence store. * @return the chosen test string. */private String chooseString(CurrentSequence sequence, SequenceStore store){ // To be implemented.}