Lecture 5
Lecture 5
Interaction Diagrams
Design
Three ways developers design objects
Code. Design-while-coding (Java, C#, …), ideally with
power tools such as re-factorings. From mental model
to code.
Draw, then code. Drawing some UML on a
whiteboard or UML CASE tool, then switching to #1
with a text-strong IDE (e.g., Eclipse or Visual Studio).
Only draw. Somehow, the tool generates everything
from diagrams.
Agile Modeling and Lightweight UML Drawing
2
Static and Dynamic Modeling
Dynamic models, such as UML interaction diagrams
(sequence diagrams or communication diagrams), help
design the logic, the behavior of the code or the method
bodies
most of the challenging, interesting, useful design work happens
while drawing the UML dynamic-view interaction diagrams
Static models, such as UML class diagrams, help design the
definition of packages, class names, attributes, and method
signatures (but not method bodies).
The most common static object modeling is with UML class
diagrams
Spend significant time doing interaction diagrams, not just
class diagrams.
it's especially during dynamic modeling that we apply
responsibility-driven design and the GRASP principles
3
Static and Dynamic Modeling
4
Object Design Skill vs. UML
Notation Skill
6
Dynamic Modeling
UML Interaction Diagrams
Interaction Diagrams
The UML includes interaction diagrams to illustrate how
objects interact via messages
The term interaction diagram is a generalization of two
more specialized UML diagram types:
sequence diagrams
communication diagrams
A related diagram is the interaction overview diagram; it
provides a big-picture overview of how a set of
interaction diagrams are related in terms of logic and
process-flow
New addition in UML 2.0
Sequence diagrams are the more notationally rich of the
two types, but communication diagrams have their use
as well, especially for wall sketching.
8
Sequence Diagram
:A m yB : B
doO ne
doTw o
d o T h re e
9
Sequence Diagram
: A m yB : B
doO ne
doTw o
d o T h re e
public class A
{
private B myB = new B();
doO ne
:A
1: doTw o
2 : d o T h re e
m yB : B
11
Interaction Diagrams
Sequence vs. Communication diagrams
Sequence Diagrams
Easier to see sequence of method calls over time
More expressive UML notation
Communication Diagrams
Better fit on limited space (page or whiteboard)
Easier to edit/amend
12
Example Sequence Diagram: makePayment
: Register : Sale
makePayment(cashTendered)
makePayment(cashTendered)
create(cashTendered) : Payment
Who created Register & Sale? IDs show a fragment of system behavior
during isolated snapshot in time. This can be confusing and lead to
modeling errors/omissions!
13
Code
public class Sale
{
private Payment payment;
public void makePayment( Money cashTendered )
{
payment = new Payment( cashTendered );
//…
}
// …
}
14
Communication Diagram
direction of message
1.1: create(cashTendered)
:Payment
15
Lifeline boxes to show participants in interactions
lifeline box representing the class
lifeline box representing a Font, or more precisely, that Font is
lifeline box representing an
named instance an instance of class Class – an
unnamed instance of class Sale
instance of a metaclass
«metaclass»
:Sale s1 : Sale
Font
List is an interface
lifeline box representing an lifeline box representing
instance of an ArrayList class, one instance of class Sale, in UML 1.x we could not use an
parameterized (templatized) to selected from the sales interface here, but in UML 2, this (or
hold Sale objects ArrayList <Sale> collection an abstract class) is legal
sales: x : List
sales[ i ] : Sale
ArrayList<Sale>
related
example
16
Interaction Diagrams
Message expression syntax
Don’t have to use full syntax in all cases
Examples:
initialize(code)
initialize
d = getProductDescription(id)
d = getProductDescription(id:ItemID)
d = getProductDescription(id:ItemID) : ProductDescription
17
Singleton Objects
1
: Register
: Store the ‘1’ implies this is a
Singleton, and accessed
doX via the Singleton pattern
doA
18
Basic Sequence Diagram Notation
: Register : Sale
doX
doA
doB
a found message
whose sender will not
be specified doC
doD
execution specification
bar indicates focus of
control typical sychronous message
shown with a filled-arrow line
19
Illustrating Reply or Returns
: Register : Sale
More common return syntax
doX
d1 = getDate
getDate
aDate
20
Messages to "self" or "this"
: R e g is te r
doX
c le a r
21
Creation of Instances
n o te th a t n e w ly c re a te d
: R e g is te r : S a le
o b je c ts a re p la c e d a t th e ir
c re a tio n "h e ig h t"
m a k e P a y m e n t(c a s h T e n d e re d )
c re a te (c a s h T e n d e re d ) : P aym ent
a u th o riz e
Dashed line for ‘create’ really not needed, though its now official UML
Use ‘create’ for calls to a constructor
22
Object Lifelines and Object Destruction
: S a le
c re a te (c a s h T e n d e re d )
: P aym ent
th e « d e s tro y » s te re o ty p e d
... m e s s a g e , w ith th e la rg e
X a n d s h o rt life lin e
« d e s tro y » in d ic a te s e x p lic it o b je c t
X d e s tru c tio n
23
Diagram Frames in UML Sequence
Diagrams
Looping
:A :B
makeNewSale
endSale
24
Conditional Messages
: Foo : Bar
xx
opt [ c o lo r = re d ]
c a lc u la te
yy
25
Conditional Messages in UML 1.x Style
Still Useful?
: Foo : Bar
xx
[ c o lo r = re d ] c a lc u la te
yy
26
Mutually Exclusive Conditional Messages
: A : B :C
doX
a lt [ x < 10 ]
c a lc u la te
[ e ls e ]
c a lc u la te
27
Iteration over a collection using relatively
explicit notation.
lineItems[i] :
: Sale This lifeline box represents one
SalesLineItem
instance from a collection of many
t = getTotal SalesLineItem objects.
t = g e tT o ta l
lo o p
s t = g e tS u b to ta l
30
Nesting of Frames
: Foo : Bar
xx
opt [ c o lo r = re d ]
lo o p (n )
c a lc u la te
31
Interaction occurrence, sd and ref frames
SD can contain frames that
reference other SDs sd AuthenticateUser
:B :C
:A :B :C
authenticate(id)
doX
doA doM1
doB
doM2
authenticate(id) ref
AuthenticateUser
ref sd DoFoo
DoFoo
:B :C
32
Messages to Classes to Invoke Static (or
Class) Methods
message to class, or a
static method call
doX
33
Polymorphic Messages and Cases
P a ym e n t {a b stra ct}
P a ym e n t is a n a b stra c t a u th o rize () {a b stra c t}
su p e rcla ss , w ith co n cre te ...
su b cla s se s th a t im p le m e n t th e
p o lym o rp h ic a u th o riz e o p e ra tio n
C re d itP a ym e n t D e b itP a y m e n t
a u th o riz e () a u th o rize ()
... ...
o b je c t in ro le o f a b stra c t
p o lym o rp h ic m e ssa g e
su p e rcla ss
:R e g is te r :P a ym e n t {a b stra c t}
doX
a u th o rize sto p a t th is p o in t – d o n ’t sh o w a n y
fu rth e r d e ta ils fo r th is m e ssa g e
:D e b itP a y m e n t :F o o :C re d itP a ym e n t :B a r
a u th o rize a u th o riz e
doA doX
doB
34 se p a ra te d ia g ra m s fo r e a c h p o ly m o rp h ic co n cre te ca se