Designing with Interaction
and Design Class Diagrams
Ch
t
15 & 16
Chapters 15 & 16
Applying UML and Patterns
Craig Larman
Oleh:
I Gede Made Karma
Two kinds of UML Interaction Diagrams
z
Sequence
Diagrams: show object interactions
arranged in time sequence,
vertically
z
Communication
Diagrams: show object
z
Communication
Diagrams: show object
interactions arranged as a flow of objects and
their links to each other,
numerically
z
Semantically equivalent, structurally different
–
Sequence diagram emphasize time ordering
–
Communication diagrams make object linkages
explicit
Interaction Diagram notation
Which would you expect to find most often in Interaction diagrams?
What do you think of “:Sale” instead of “aSale”?
Sequence diagram notation
: Register : Sale
doA
doB doX
doB
doC
doD
typical
sychronous
message shown with a filled-arrow line afound message
whose sender will not be specified
execution specification
bar indicates focus of controlFigure 15.7
What does vertical placement
communicate?
: Register : Sale note that newly created objects are placed at their creation "height"
makePayment(cashTendered)
: Payment create(cashTendered)
authorize
Figure 15.10
Communication Diagram:
makePayment
1: makePayment(cashTendered) :Register :Sale makePayment(cashTendered)
direction of message
1.1: create(cashTendered) :Register :Sale
:Payment
Figure 15.4
What do the numbers communicate?
Communication
(aka Collaboration) diagrams
z
Objects are rectangular icons
–
e.g., Order Entry Window, Order, etc.
z
Messages are arrows between icons
Messages are arrows between icons
–
e.g., prepare()
z
Numbers on messages indicate sequence
–
Also spatial layout helps show flow
z
Which do you prefer: sequence or communication?
z
Fowler doesn’t use
communication
diagrams
–
Show flow clearly, but awkward modeling alternatives
z
UML notation for control logic has changed in UML 2
but Fowler isn’t impressed
Control logic in
Interaction Diagrams
z
Conditional Message
–
[ variable = value ]
: message()
M
i
l if l
l
t
–
Message is sent only if clause evaluates to
true
z
Iteration (Looping)
–
* [ i := 1..N ]
: message()
–
“*” is required; [ ... ] clause is optional
z
Communication diagrams add Seq. Numbers
before conditional messages or loops
Logic in sequence diagrams:
which notation do you prefer?
calculate
: Bar xx
[ color = red ]
opt
: Foo
yy
[ color = red ] calculate
: Bar
yy xx : Foo
Figure 15.13
Figure 15.14
Logic in communication diagrams
conditional message, with test
1
[ color = red ]
: calculate
: Foo
: Bar
message1
Figure 15.29
Loops in sequence diagrams:
which notation do you prefer?
st = getSubtotal
lineItems[i] : SalesLineItem t = getTotal
[ i < lineItems.size ] loop
: Sale This lifeline box represents one instance from a collection of many SalesLineItem objects. lineItems[i] is the expression to select one element from the collection of many SalesLineItems; the ‘i” value refers to the same “i” in the guard i th LOOP f i++
Figure 15.16
Figure 15.17
in the LOOP frame an action box may contain arbitrary language
statements (in this case, incrementing ‘i’) it is placed over the lifeline to which it applies
st = getSubtotal
lineItems[i] : SalesLineItem t = getTotal
loop
: Sale
Iteration in communication diagrams
1 * [i = 1..n]: st = getSubtotal : Sale
t = getTotal
This lifeline box represents one instance from a collection of many SalesLineItem objects. lineItems[i] is the expression to select one element from the collection of many
lineItems[i]: SalesLineItem
this iteration and recurrence clause indicates we are looping across each element of the lineItems collection.
element from the collection of many SalesLineItems; the ‘i” value comes from the message clause.
1 *: st = getSubtotal : Sale
t = getTotal lineItems[i]: SalesLineItem
Less precise, but usually good enough to imply iteration across the collection members
Asynchronous calls
3: runFinalization
:ClockStarter
System : Class
startClock
y
:Clock
1: create
2: run
asynchronous message
active object
Figure 15.35
What’s the difference between synchronous and asynchronous?
Active object runs in its own thread of execution
Polymorphism:
How is it shown in interaction diagrams?
:Register authorize
doX :Payment {abstract} polymorphic message
object in role of abstract superclass stop at this point – don’t show any further details for this message
g y { } superclass
:DebitPayment authorize
:Foo
separate diagrams for each polymorphic concrete case doA
doB :CreditPayment authorize
:Bar doX
Figure 15.34
Design Class Diagrams (DCDs)
z
During analysis, emphasize domain concepts
z
During design, shift to software artifacts
z
UML has no explicit notation for DCDs
z
Uniform UML notation supports smoother
development from analysis to design
Domain model vs.
Design Class Diagram – differences?
Register
Sale
time Captures 1 1
Domain Model
conceptual
Register
...
endSale() enterItem(...) makePayment(...)
Sale
time
isComplete : Boolean /total
makeLineItem(...)
... isComplete : Boolean
/total
1 conceptual
perspective
Design Model
DCD; software perspective
currentSale
Figure 16.2
Developing a Domain Class Diagram:
the NextGen POS DCD
1) Identify
software classes
:
Register Sale
P d
C
l
P d
S
ifi
i
ProductCatalog
ProductSpecification
Store SalesLineItem
Payment
2) Begin drawing a
class diagram
3) Include the
attributes from
the domain model
How to show attribute collections?
Sale time: DateTime lineItems : SalesLineItem [1..*] or
li It S l Li It [1 *] { d d}
SalesLineItem ... ...
notice that an association end can optionally also have a property string such as {ordered, List} Sale
time: DateTime ...
SalesLineItem ... ... 1..
*
lineItems {ordered, List} lineItems : SalesLineItem [1..*] {ordered} ...
Two ways to show a collection attribute
4) Add method names
—from interaction diagrams
—model class & interaction diagrams in parallel
: Register : Sale makePayment(cashTendered)
makePayment(cashTendered)
Register ... makePayment(…) ...
Sale ... makePayment(…) ... 1 currentSale messages in interaction
diagrams indicate operations
in the class diagrams classes identified in the interaction diagrams are declared in the class diagrams
Parameters, return types optional?
—readability vs. code generation
Method body pseudo-code also optional
Register «method»
// pseudo-code or a specific language is OK ... endSale() enterItem(id, qty) makeNewSale() makePayment(cashTendered) // pseudo code or a specific language is OK
public void enterItem( id, qty ) {
ProductDescription desc = catalog.getProductDescription(id); sale.makeLineItem(desc, qty);
}
5) Add associations and navigability
—Navigability implies visibility of attributes
How does
navigability
l if thi
What attribute does
ProductCatalog
implicitly contain?
clarify this
design?
6) Adding dependency relationships
z
Indicates that one element has knowledge of
another element
z
I.e., a change in specification of one thing may
affect another thing that uses it, but not
necessarily the reverse
z
A dashed directed line
z
Typically non-attribute visibility between classes
What does dependency add to this DCD?
ProductDescription
the Sale has parameter visibility to a
ProductDescription, and thus some kind of
dependency
SalesLineItem
...
...
ProductDescription
...
...
1..
*
lineItems
Sale
...
Composition (whole-part) relations
Finger 0..7 Hand
1 composition means
-a part instance (Square) can only be part of one composite (Board) at a time composition Square 40 Board 1 SalesLineItem 1..* Sale 1 composite (Board) at a time
-the composite has sole responsibility for management of its parts, especially creation and deletion
Figure 16.13
Association classes
—model association with attributes & operations
Employs
Company
*
*
Personsalary startDate
Employment a person may have
employment with several companies
Figure 16.16
«interface» List clear() ... K parameterized or templateinterfaces and classes K is a template parameter
the attribute type may be expressed in official UML, with the template binding syntax requiring an arrow or
in another language, such as Java
Interfaces and Template Classes
—Interface is a predefined «stereotype»
—Templates take parameters in corner
anonymous class with template binding complete
Board squares : List<K Square> or squares : List<Square> ...
ArrayList<T Square> clear() ...
ArrayList elements : T[*] ... clear() ...
T
for example, the elements attribute is an array of type T, parameterized and bound before actual use.
there is a chance the UML 2 “arrow” symbol will eventually be replaced with something else e.g., ‘=’
Figure 16.18
DCD
summary
java.awt::Font or java.awt.Font plain : Int = 0 { readOnly } bold : Int = 1 { readOnly } name : String style : Int = 0 ... getFont(name : String) : Font getName() : String ... SuperclassFooor SuperClassFoo { abstract } - classOrStaticAttribute : Int + publicAttribute : String - privateAttribute assumedPrivateAttribute isInitializedAttribute : Bool = true aCollection : VeggieBurger [ * ] attributeMayLegallyBeNull : String [0..1] finalConstantAttribute : Int = 5 { readOnly } /derivedAttribute + classOrStaticMethod() + publicMethod() assumedPublicMethod() - privateMethod() # protectedMethod() ~ packageVisibleMethod() «constructor» SuperclassFoo( Long ) methodWithParms(parm1 : String, parm2 : Float) 3 common
compartments 1. classifier name 2. attributes 3. operations
officially in UML, the top format is used to distinguish the package name from the class name unofficially, the second alternative is common
an interface shown with a keyword
Questions?
«interface»Runnable run()
- ellipsis “…” means there may be elements, but not shown - a blank compartment officially means “unknown” but as a convention will be used to mean “no members”
SubclassFoo ...
run() ...
methodReturnsSomething() : VeggieBurger methodThrowsException() {exception IOException} abstractMethod()
abstractMethod2() { abstract } // alternate finalMethod() { leaf } // no override in subclass synchronizedMethod() { guarded }
interface implementation and subclassing Fruit ... ... PurchaseOrder ... ... 1 association with multiplicities dependency order
Figure 16.1
Designing with interaction
and class diagrams
z
Beginners often emphasize Class diagrams
z
Interaction diagrams deserve more attention
z
Some tools can help:
–
Convert between sequence and communication
diagrams automatically