Title: UML Class Diagrams Sequence Diagrams
1UML Class DiagramsSequence Diagrams
2Agenda
- Class Diagrams
- Symbology
- Basic Modeling
- Intermediate Modeling
- Sequence Diagrams
- Symbology
3Class Diagrams
- A class diagram shows
- Classes
- Attributes
- Methods
- Interfaces
- Collaborations
- Relationships Dependency, Generalization,
Association - A class diagram is a STATIC view of system
4Basic Class Diagrams
Window
Class Name
size Sizevisibility boolean
Attributes
display()hide()
Operations
5Basic Class Diagrams
public protected - private / derived
static
Abstract ltltabstractgtgt
ltltconstructorgtgt ltltquerygtgt ltltupdategtgt
Class Scope Variable
Visibility Attribute Name MultiplicityType
Initial Value Visibility Method Name (Parameter
List) Return-List
6Attribute Example (Java implementation)
public class Note public String author
unknown public String text private static
long total 0 ...
7Operation Example (Java implementation)
public class Figure private Size size
private Position pos private static long
figureCount 0 public void move(Position
pos) ... public static long
getFigureCount() return figureCount ...
8Basic Class Diagrams
Superclass
Class with parts
Class with parts
Class A
Interface
name
Subclass
Assembly Class
Assembly Class
Class B
Concrete Class
Inheritance (Generalization) (is-a, kind-of)
Aggregation (Part-Of)
Association (relationship)
Dependency
Realization
9Associations
- A semantic relationship between two or more
classes that specifies connections among their
instances - A structural relationship specifying that objects
of one class are connected to objects of a second
(possibly the same) class - Example An Employee works for a Company
10Associations (cont.)
- 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
3
1
Book
currBorr
bk
11Association(Java implementation)
- public class Borrower
-
- Book bk
- int numBooks
-
- public Borrower()
- numBooks 0
- bk new Book3
-
-
- // methods that update bk
- public void borrowBook( Book b )
- bknumBooks b
- numBooks
- b.setBorrower( this )
-
- public class Book
-
- Borrower currBorr
- public void setBorrower( Borrower bw )
- currBorr bw
-
-
12Aggregation
- 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.
4
Wheel
wheels
Whole
Part
13Aggregation(Java implementation)
- public class Car
- private Wheel wheels
- ...
- // wheel objects are created externally and
- // passed to the constructor
- public Car( Wheel w1, Wheel w2, )
-
- // we can check w1, w2, etc. for null
- // to make sure wheels exist
- wheels new Wheel4
- wheels0 w1
- wheels1 w2
-
-
14Composition
- 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 one
- The life time of the part is dependent upon the
whole - The composite must manage the creation and
destruction of its parts
15Composition(C implementations)
- class Circle public Circle() center(1,2)
private Point center - class Circle public Circle() center(new
Point(1,2)) Circle() delete center
private Point const center
16Composition(Java implementations)
- public class Car
- private Wheel wheels
- ...
- public Car()
-
- wheels new Wheel4
- // Wheels are created in Car
- wheels0 new Wheel()
- wheels1 new Wheel()
-
-
- ...
17Generalization
- Indicates that objects of the specialized class
(subclass) are substitutable for objects of the
generalized class (super-class) - is kind of relationship
Shape
Super Class
An abstract class
Generalization relationship
Sub Class
Circle
18Generalization
- 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
19Generalization(Java implementation)
- public abstract class Shape public abstract
void draw() ... - public class Circle extends Shape public
void draw() ... ...
20Dependency
- A dependency indicates a semantic relation
between two or more classes in which a change in
one may force changes in the other although there
is no explicit association between them - A stereotype may be used to denote the type of
the dependency
21Dependency(Java implementation)
- public class Bank
-
-
- public void processTransactions()
-
- // Parser p is a local variable
- Parser p new Parser()
-
- p.getTransaction()
-
-
-
22Realization
- 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
ltltinterfacegtgtList
LinkedList
List
23Realization(Java implementation)
- public interface List boolean add(Object o)
... - public class LinkedList implements List
public boolean add(Object o) -
- ...
- ...
24Basic Class Diagram (Example)
takes
25Basic Class Diagram (Example)
Person
name String - ssn String birthday
Date / age int
getName String -calculateAge int
26Class Diagrams (Advanced)
Cardinality (Multiplicity) 1 0..1 0..n 1..n
takes
0..n
27Class Diagrams (Advanced)
Important Stereotypes ltltinterfacegtgt specify
collection of operations to specify
services ltlttypegtgt specify structure and
behavior (not implementation) ltltenumerationgtgt
specify discrete values ltltimplementationClassgtgt
helper class created in detail design
ltltenumerationgtgt Status
Idle Working Error
Fundamental Attributes
readImage writeImage
Fundamental behavior
28Class Diagrams (Advanced)
Simple Aggregation (object lifetime)
Composite Aggregation (unique member)
Can have self-relations
Exception handling
ltltsendsgtgt
manager
manages
employee
29Class Diagrams (Advanced)
Association Class
30TVRS Example
TrafficReport
Offender
issues
1
TrafficPoliceman
1..
1
id long
name String
description String
id long
occuredAt Date
reports of
1..
Policeman
Violation
id long
name String
id long
rank int
description String
ltltabstractgtgt
31Class Diagrams (Advanced)
32Class Diagram Hints
- Provide abstraction of problem domain
- Embodies small set of well-defined
responsibilities - Clear separation between specification and
implementation - Understandable and simple
- Show only important properties
33Class Hints
- Organize similar classes into packages
- Beware of cyclical generalization
- Use associations where there are structural
relationships - Associations are NOT comm pipes!!!!!!!
- Start with Analysis, then refine details
34Sequence Diagrams
- AKA Interaction Diagrams Semantically
equivalent to Collaboration Diagrams - Dynamic Model relating use cases and class
diagrams - Illustrates how objects interacts with each other
- Shows time ordering of interactions
- Generally a set of messages between collaborating
objects - Ordering of objects not significant
35Sequence Diagrams
- Show only one flow of control
- Can model simple sequential flow, branching,
iteration, recursion and concurrency - May need multiple diagrams
- Primary
- Variant
- Exceptions
36Sequence Diagram (Basic)
Object Class or Actor
name
Focus of Control/ Activation
message
Object Destruction/ Termination
X
ltltcreategtgt ltltdestroygtgt
37Sequence Diagram (Basic)
aClass Class
Register
adjustRoom
checkRooms
38Sequence Diagram (Basic)
X-Axis (objects)
memberLibraryMember
Object
Life Line
Y-Axis (time)
message
Activation box
condition
39Sequence Diagrams (Advanced)
Seq Guard Iteration Return-List
Operation-Name (Argument-List)
Conditional Lifeline
transient
40Object
- Object naming
- syntax instanceNameclassName
- Name classes consistently with your class diagram
(same classes). - Include instance names when objects are referred
to in messages or when several objects of the
same type exist in the diagram. - The Life-Line represents the objects life during
the interaction
41Messages
- An interaction between two objects is performed
as a message sent from one object to another
(simple operation call, Signaling, RPC) - If object obj1 sends a message to another object
obj2 some link must exist between those two
objects (dependency, same objects)
42Messages (Cont.)
- A message is represented by an arrow between the
life lines of two objects - Self calls are also allowed
- The time required by the receiver object to
process the message is denoted by an
activation-box - A message is labeled at minimum with the message
name - Arguments and control information (conditions,
iteration) may be included
43Return Values
- Optionally indicated using a dashed arrow with a
label indicating the return value. - Dont model a return value when it is obvious
what is being returned, e.g. getTotal() - Model a return value only when you need to refer
to it elsewhere, e.g. as a parameter passed in
another message. - Prefer modeling return values as part of a method
invocation, e.g. ok isValid()
44Sequence Diagram (Basic)
Clerk
lookup
viewButton()
idgetID()
getViolation(id)
May be a pseudo-method
ltltcreategtgt
v
display(v)
45Sequence Diagram (Basic)
Active object
Client
print(doc,client)
enqueue(job)
Repeated forever with 1 min interludes
jobprint(job.doc)
status
job done(status)