cs212: object oriented analysis and design lecture 33: class and sequence diagram
TRANSCRIPT
CS212: Object Oriented Analysis and Design
Lecture 33: Class and Sequence Diagram
Recap of Lecture 32
• Introduction to Modeling
• Identifying use case
• Role
• Use case diagram
Outline of Lecture 33
• Class diagram
• Sequence Diagram
Class Diagram
• Use the class diagram to model the static structure of a system
• Describes the elements of the system and the relationships between them
• Class diagram allows to create a conceptual view of the system and to define the vocabulary to be used
Essential features
• Class
• Attributes
• Operations
• Relationships• Associations• Generalization• Realization
• Dependency
• Constraint Rules and Notes
Class
• Describes a set of objects having similar:• Attributes (status)• Operations (behavior)• Relationships with other classes
• Attributes and operations may• have their visibility marked:• "+" for public• "#" for protected• "−" for private• "~" for package
Example
Class Name
Attributes
Operations
Associations
• An association between two classes indicates that objects at one end of an association “recognize” objects at the other end and may send messages to them.
• Example: “An Employee works for a Company”
Employee Company
Associations (cont.)
• To clarify its meaning, an association may be named.• The name is represented as a label placed midway along the
association line.
• Usually a verb or a verb phrase.
• A role is an end of an association where it connects to a class.• May be named to indicate the role played by the class
attached to the end of the association path.
• Usually a noun or noun phrase• Mandatory for reflexive associations
Associations (cont.)
• Multiplicity• the number of objects that participate in the association.• Indicates whether or not an association is mandatory.
Exactly one 1
Zero or more (unlimited) * (0..*)
One or more 1..*
Zero or one (optional association) 0..1
Specified range 2..4
Multiple, disjoint ranges 2, 4..6, 8
Multiplicity Indicators
Associations: Example
StaffMember Student1..* *instructs
instructor
Association name
Role name
MultiplicityNavigable
(uni-directional) association
Courses
pre - requisites
0..3Reflexive
association
Role
*
Aggregation
• A special form of association that models a whole-part relationship between an aggregate (the whole) and its parts.
• Models a “is a part-part of” relationship.
Whole Part
Car Door House1..*2..*
Aggregation (cont.)
• Aggregation tests:• Is the phrase “part of” used to describe the relationship?
• A door is “part of” a car
• Are some operations on the whole automatically applied to its parts?
• Move the car, move the door.
• Are some attribute values propagated from the whole to all or some of its parts?
• The car is blue, therefore the door is blue.
• Is there an intrinsic asymmetry to the relationship where one class is subordinate to the other?
• A door is part of a car. A car is not part of a door.
Composition
• A strong form of aggregation• The whole is the sole owner of its part.
• The part object may belong to only one whole
• Multiplicity on the whole side must be zero or one.
• The life time of the part is dependent upon the whole. • The composite must manage the creation and
destruction of its parts.
Circle Point
3..*
1
PolygonPoint
Circle
Generalization• Indicates that objects of the specialized class (subclass) are
substitutable for objects of the generalized class (super-class).
• “is kind of” relationship.
Shape{abstract}
Circle
Super Class
Sub Class
An abstract class
Generalization relationship
{abstract} is a tagged value that indicates that the class is abstract. The name of an abstract class should be italicized
Generalization
• A sub-class inherits from its super-class• Attributes• Operations• Relationships
• A sub-class may• Add attributes and operations• Add relationships• Refine (override) inherited operations
• A generalization relationship may not be used to model interface implementation.
Realization
• A realization relationship indicates that one class implements a behavior specified by another class (an interface or protocol).
• An interface can be realized by many classes.
• A class may realize many interfaces.
LinkedList<<interface>>
List ArrayList
Dependency
• Dependency is a weaker form of relationship which indicates that one class depends on another because it uses it at some point in time.
• One class depends on another if the independent class is a parameter variable or local variable of a method of the dependent class.
• This is different from an association, where an attribute of the dependent class is an instance of the independent class.
Iterator Vector<<friend>>
Constraint Rules and Notes
• Constraints and notes annotate among other things associations, attributes, operations and classes.
• Constraints are semantic restrictions noted as Boolean expressions.
• UML offers many pre-defined constraints.
id: long { value > 0 }
CustomerOrder*1
{ total < $50 }may be canceled
Constraint Note
The Sequence Diagram
• The interactions between the objects in a system
• An interaction specifies how messages and data are exchanged between interaction partners.
• Interaction partners: human, non-human, server, printers, etc..
• The sequence diagram is a two-dimensional diagram
Importance
• Depict object interactions in a given scenario identified for a given Use Case
• Specify the messages passed between objects using horizontal arrows including messages to/from external actors
• Time increases from Top to bottom
Rules of Sequence Diagrams
• Sequence Initiation
updateStatus( )
Click Update Button
User
Object1:C1 Object2:C2
Rules of Sequence Diagrams
• Identify objects needed to support use case, determine sequence of internal events following the external initiating event
• Diagrams that are not initiated with an external actor represent only a partial sequence
• Partial sequence diagrams should clearly identify the actor initiated sequence diagrams from which they are launched
Structure of a sequencediagram
Rules of Sequence Diagrams
• Messages specified on interactions can be synchronous or asynchronous
updateStatus( )
Object1:C1 Object2:C2
calcRoute( )
route
Object1:C1 Object2:C2
Implicit Returns Explicit Return
Object3:C3
generateMessage( )
Synchronous call
Rules of Sequence Diagrams
Asynchronous call
asynchronous_Call ()
callBack ()
Object1:C1{active}
Object2:C2{active}
Rules of Sequence Diagrams
• Display operation names on call arrows
c:clientfactory:
cmd_pktCToCHeaderFactory
header:cmd_pktCToCCommandHeaderbuildHeader( char * )
«create»
header
Specifying Timing Requirements
a: Command
Create Transaction
b: Command
d: ResponseRemove Transaction
c: Response
CMM : process target : process
User Application
{b.receiveTime a.sendTime + 50ms}
{d.receiveTime a.sendTime + 1s}
Example: Use Case Diagram of the Vending Machine
C u s to m e r
D ep o s it M o n ey
M ak e S e lec tio n
M a in te n a n ceO pe ra to r
Up d ate D atab as e
C an c ella tio n
The Initial Object-Model
V e n din g M a ch in eC o n tro lle r
Nu m e ric K e y pa d
W ire le s sK e y bo a rd
C R T D is pla y
M o n e y D e te ct io nH a rdwa re
M o n e y D is pe n s e r
Pro du ctD is pe n s e r
Pro du ct D a ta ba s e
Designate the Actor objects with the <<actor>> stereotype above the object label
<<actor>>
<<actor>>
<<actor>>
<<actor>>
<<actor>>
<<actor>>
Deposit Money Use Case
C u s to m e r
M o n e y D e te c t io nH a rd w a re
CRT D is p la yVe n d in g M a c h in e
Co n t ro lle r
D e p o s it M o n e y
[Id le S ta te ]D is p la y W e lc o meM e s s a g e
D is p la y A mo u n t
S e n d A mo u n t
P ro mp t fo r S e le c t io n
S ta rt T ime r
[If T ime r > 30 s e c o n d s a n d In v a lid o r N o S e le c t io n ] Re tu rn M o n e y
M o n e yD is p e n s e r
S to pT ime r[Id le S ta te ]
D is p la y W e lc o meM e s s a g e
Thank youNext Lecture: UML Diagrams