8/10/2019 Interaction and design class diagrams
1/29
Designing with Interactionand Design Class Diagrams
Chapters 15 & 16
Applying UML and Patterns
Craig Larman
With some ideas from
students in George Blanks
course at NJIT
8/10/2019 Interaction and design class diagrams
2/29
Two kinds of UML Interaction Diagrams
SequenceDiagrams: show object interactionsarranged in time sequence, vertically
CommunicationDiagrams: show objectinteractions arranged as a flow of objects andtheir links to each other, numerically
Semantically equivalent, structurally different Sequence diagram emphasize time ordering
Communication diagrams make object linkagesexplicit
8/10/2019 Interaction and design class diagrams
3/29
Interaction Diagram notation
class instance named instance
:Sale s1:SaleSale
Which would you expect to find most often in Interaction diagrams?
What do you think of :Saleinstead of aSale?
8/10/2019 Interaction and design class diagrams
4/29
Sequence diagram notation
: Register : Sale
doA
doB
doX
doC
doD
typicalsychronousmessage
shown with a filled-arrowline
afound messagewhose sender will notbe specified
execution specificationbar indicates focus ofcontrol
Figure 15.7
8/10/2019 Interaction and design class diagrams
5/29
What does vertical placement
communicate?
: Register : Sale
makePayment(cashTendered)
: Paymentcreate(cashTendered)
authorize
notethat newlycreatedobjectsareplacedat theircreation"height"
Figure 15.10
8/10/2019 Interaction and design class diagrams
6/29
Communication Diagram:
makePayment
1: makePayment(cashTendered)
1.1: create(cashTendered)
:Register :Sale
:Payment
makePayment(cashTendered)
directionof message
Figure 15.4
What do the numbers communicate?
What does a create message communicate?
8/10/2019 Interaction and design class diagrams
7/29
Communication
(aka Collaboration) diagrams
Objects are rectangular icons
e.g., Order Entry Window, Order, etc.
Messages are arrows between icons
e.g., prepare()
Numbers on messages indicate sequence
Also spatial layout helps show flow
Which do you prefer: sequence or communication?
Fowler doesnt use communication diagrams
Show flow clearly, but awkward modeling alternatives
UML notation for control logic has changed in UML 2
but Fowler isnt impressed
http://www.cse.lehigh.edu/~glennb/oose/figs/collab5-4.jpghttp://www.cse.lehigh.edu/~glennb/oose/figs/collab5-4.jpghttp://www.cse.lehigh.edu/~glennb/oose/figs/collab5-4.jpghttp://www.cse.lehigh.edu/~glennb/oose/figs/collab5-4.jpghttp://www.cse.lehigh.edu/~glennb/oose/figs/collab5-4.jpg8/10/2019 Interaction and design class diagrams
8/29
Control logic in
Interaction Diagrams
Conditional Message
[ variable = value ]: message() Message is sent only if clause evaluates to true
Iteration (Looping)
* [ i := 1..N ]: message()
* is required; [ ... ] clause is optional
Communication diagrams add Seq. Numbers
before conditional messages or loops
8/10/2019 Interaction and design class diagrams
9/29
Logic in sequence diagrams:which notation do you prefer?
calculate
: Bar
yy
xx
[ color =red]opt
: Foo
[ color =red] calculate
: Bar
yy
xx
: Foo
Figure 15.13
Figure 15.14
8/10/2019 Interaction and design class diagrams
10/29
Logic in communication diagrams
1[ color =red]: calculate
: Foo : Bar
message1
conditional message, withtest
Figure 15.29
8/10/2019 Interaction and design class diagrams
11/29
Loops in sequence diagrams:
which notation do you prefer?
Figure 15.16
Figure 15.17
st =getSubtotal
lineItems[i]:SalesLineItem
t =getTotal
[ i
8/10/2019 Interaction and design class diagrams
12/29
Iteration in communication diagrams
1 * [i =1..n]: st =getSubtotal: Sale
t =getTotal
This lifelinebox represents oneinstancefromacollectionof manySalesLineItemobjects.
lineItems[i]is theexpressiontoselect oneelement fromthecollectionof manySalesLineItems;thei valuecomes fromthemessageclause.
lineItems[i]:SalesLineItem
this iterationandrecurrenceclauseindicatesweareloopingacross eachelement of thelineItemscollection.
1 *: st =getSubtotal: Salet =getTotallineItems[i]:
SalesLineItem
Less precise, but usually goodenoughtoimplyiterationacross thecollectionmembers
Figure 15.31
8/10/2019 Interaction and design class diagrams
13/29
Asynchronous calls
3: runFinalization:ClockStarter System: Class
startClock
:Clock
1: create
2: run
asynchronousmessage
activeobjectFigure 15.35
Whats the difference between synchronous and asynchronous?
Active object runs in its own thread of execution
8/10/2019 Interaction and design class diagrams
14/29
Polymorphism:
How is it shown in interaction diagrams?
:RegisterauthorizedoX
:Payment {abstract}
polymorphicmessage
object inroleof abstractsuperclass
:DebitPayment
authorize
:Foo
stopat thispointdont showanyfurther detailsfor thismessage
separatediagramsfor eachpolymorphicconcretecase
doAdoB
:CreditPayment
authorize
:BardoX
Figure 15.34
8/10/2019 Interaction and design class diagrams
15/29
Chapter 16
Design Class Diagrams (DCDs)
During analysis, emphasize domain concepts
During design, shift to software artifacts UML has no explicit notation for DCDs
Uniform UML notation supports smoother
development from analysis to design
8/10/2019 Interaction and design class diagrams
16/29
Domain model vs.
Design Class Diagramdifferences?
Register
...
endSale()enterItem(...)makePayment(...)
Sale
timeisComplete: Boolean
/total
makeLineItem(...)
Register
...
Sale
timeisComplete: Boolean
/total
Captures
1
11
DomainModel
conceptualperspective
DesignModel
DCD; softwareperspective
currentSale
Figure 16.2
8/10/2019 Interaction and design class diagrams
17/29
Developing a Domain Class Diagram:
the NextGen POS DCD
1) Identify software classes:
Register SaleProductCatalog ProductSpecification
Store SalesLineItem
Payment
2) Begin drawing a class diagram3) Include the attributes from the domain model
8/10/2019 Interaction and design class diagrams
18/29
How to show attribute collections?
notice that an association end can optionally alsohave a property string such as {ordered, List}
Sale
time: DateTime
...
SalesLineItem
...
...
1..*
lineItems{ordered, List}
Sale
time: DateTimelineItems : SalesLineItem[1..*]
orlineItems : SalesLineItem[1..*] {ordered}
...
SalesLineItem
...
...
Two ways to showacollection attribute
Figure 16.4
8/10/2019 Interaction and design class diagrams
19/29
4) Add method namesfrom interaction diagramsmodel class & interaction diagrams in parallel
: Register : Sale
makePayment(cashTendered)
makePayment(cashTendered)
Register
...
makePayment()
...
Sale
...
makePayment()
...
1
currentSale
messages ininteractiondiagrams indicateoperationsintheclass diagrams classes
identifiedintheinteractiondiagrams aredeclaredintheclass diagrams
8/10/2019 Interaction and design class diagrams
20/29
Parameters, return types optional?
readability vs. code generation
8/10/2019 Interaction and design class diagrams
21/29
Method body pseudo-code also optional
Register
...
endSale()enterItem(id, qty)makeNewSale()makePayment(cashTendered)
method// pseudo-codeor aspecificlanguageisOKpublicvoidenterItem( id, qty){ProductDescriptiondesc=catalog.getProductDescription(id);sale.makeLineItem(desc, qty);}
8/10/2019 Interaction and design class diagrams
22/29
5) Add associations and navigabilityNavigability implies visibility of attributes
How does
navigabilityclarify this
design?
What attribute does
ProductCatalog
implicitly contain?
8/10/2019 Interaction and design class diagrams
23/29
6) Adding dependency relationships
Indicates that one element has knowledge of
another element I.e., a change in specification of one thing may
affect another thing that uses it, but not
necessarily the reverse
A dashed directed line
Typically non-attribute visibility between classes
8/10/2019 Interaction and design class diagrams
24/29
What does dependency add to this DCD?
SalesLineItem
...
...
ProductDescription
...
...
1..*lineItems
Sale
...
updatePriceFor( ProductDescription)
...
theSalehas parameter visibility toaProductDescription, andthus somekindofdependency
8/10/2019 Interaction and design class diagrams
25/29
Composition (whole-part) relations
Finger0..7
Hand
composition
1
Square40
Board1
SalesLineIte1..*
Sale1
compositionmeans
-apart instance(Square) canonlybepart of onecomposite(Board) at atime
-thecompositehassoleresponsibilityfor managemitsparts, especiallycreationanddeletion
Figure 16.13
8/10/2019 Interaction and design class diagrams
26/29
Association classes
model association with attributes & operations
salary
startDate
Employment
EmploysCompany Person**
apersonmayhaveemployment withseveralcompanies
Figure 16.16
8/10/2019 Interaction and design class diagrams
27/29
interfaceList
clear()
...
Kparameterizedor templateinterfacesandclasses
Kisatemplateparameter
anonymousclasswith
templatebindingcomplete
Board
squares: List
orsquares: List
...
ArrayList
clear()...
theattributetypemaybeexpressedin
official UML, withthetemplatebinding
syntaxrequiringanarrow
orinanother language, suchasJava
ArrayList
elements: T[*]...
clear()
...
T
for example, theelementsattributeisanarrayof typeT, parameterizedandboundbeforeactual use.
thereisachancetheUML2arrow symbol will
eventuallybereplacedwithsomethingelsee.g.,=
Interfaces and Template ClassesInterface is a predefined stereotype
Templates take parameters in corner
Figure 16.18
o f f i c ia l l y i n U M L t h e t o p f o r m a t i s
8/10/2019 Interaction and design class diagrams
28/29
DCD
summary
Questions?
ja v a .a w t: :F o n t
o rja v a .a w t. F o n t
p l a in : In t = 0 { r e a d O n l y }b o ld : I nt = 1 { r ea d O n l y }n a m e : S t r i n gs t y l e : I n t = 0.. .
g e t F o n t ( n a m e : S t ri n g ) : F o n tg e tN a m e ( ) : S t ri n g
.. .
i n t e r f a c e R u n n a b l e
r u n ( )
- e l li p s is m e a n s t h e r e m a y b e e l e m e n t s , b u t n o t s h o w n
- a b l a n k c o m p a r t m e n t o f fi c ia l ly m e a n s u n k n o w n b u t a s ac o n v e n t i o n w i ll b e u s e d t o m e a n n o m e m b e r s
S u b c l a s s F o o
.. .
r u n ( ).. .
S u p e r c l a s s F o oo r
S u p e r C l a s s F o o { a b s t r a c t }
- c l a s s O r S t a t ic A t t r ib u t e : I n t+ p u b l i c A t t r ib u t e : S t r in g- p r iv a t e A t t ri b u tea s s u m e d P r i v a t e A t t r i b u t ei s I n i ti a l iz e d A t t ri b u t e : B o o l = t r u e
a C o l l e c t io n : V e g g i e B u r g e r [ * ]a t t r i b u t e M a y L e g a l l y B e N u l l : S t r i n g [ 0 . . 1 ]f i n a l C o n s t a n t A t t r ib u t e : In t = 5 { re a d O n l y }
/d e ri v e d A tt ri b u te
+ c l a s s O r S t a t ic M e t h o d ( )+ p u b l i c M e t h o d ( )a s s u m e d P u b l i c M e t h o d ( )- p r iv a t eM e t h o d ( )# p r o t e c te d M e t h o d ( )~ p a c k a g e V i s i b l e M e t h o d ( )
c o n s t r u c to r S u p e r c l a s s F o o ( L o n g )m e t h o d W i th P a r m s ( p a r m 1 : S t r in g , p a r m 2 : F l o a t)m e t h o d R e t u r n s S o m e t h i n g ( ) : V e g g i e B u r g e rm e t h o d T h r o w s E x c e p t i o n ( ) { e x c e p t i o n I O E x c e p t i o n }a b s t ra c t M e t h o d ( )a b s t r a c t M e t h o d 2 ( ) { a b s t r a c t } / / a l t e r n a t ef i n a l M e t h o d ( ) { l e a f } / / n o o v e r r i d e i n s u b c l a s ss y n c h r o n i z e d M e t h o d ( ) { g u a r d e d }
3 c o m m o nco m p a r t m e n t s
1 . c l a s s i f ie r n a m e
2 . a t t r i b u t e s
3 . o p e r a t i o n s
i n t e r f a c ei m p l e m e n t a t i o na n ds u b c l a s s i n g
F r u i t
.. .
.. .
P u r c h a s e O r d e r
.. .
.. .
1
a s s o c i a t i o n w i t hmu l t i p l i c i t i e s
d e p e n d e n c y
o f f i c ia l l y i n U M L , t h e t o p f o r m a t i su s e d t o d i s ti n g u i s h t h e p a c k a g en a m e f ro m t h e c la s s n a m e
u n o f f i c i a l ly , t h e s e c o n d a l t e r n a t i v ei s c o m m o n
o r d e r
a n i n t e r f a c es h o w n w i th ak e y w o r d
Figure 16.1
8/10/2019 Interaction and design class diagrams
29/29
Designing with interaction
and class diagrams
Beginners often emphasize Class diagrams
Interaction diagrams deserve more attention Some tools can help:
Convert between sequence and communication
diagrams automatically
Reflect changes in class and interaction diagramsin parallel
Top Related