Title: Interaction Diagrams
1Chapter 15
2Most Common
- Sequence Diagram
- Communication Diagram
- Sequence Diagrams illustrate interactions between
classes of a program using class methods as
message names,
3Sample Code
public class A private B myB null
public A () myB new B()
public void doOne() myB.doTwo()
myB.doThree()
4Fig. 15.1
duration of doOne() activity
5Fig. 15.2
Communication Diagram
sequence numbers
Same interaction in a network diagram
description
6Strengths and Weaknesses
- UML has put more thought into SDs
- Better tool support for SDs
- Better at showing sequence
- CDs are more space efficient, boxes can go
anywhere - Easier to modify a CD
- CDs are vertical so fit on narrow pages of a
book
7Fig. 15.3
lifeline
NOTE If you need to extend youve run out of
space
8Fig. 15.4
NOTE If you need to extend you can come back
to the left.
9Static vs Dynamic View
- The Class Diagram is a static view of the program
while the Sequence Diagram is a dynamic view.
Both are useful.
10Fig. 15.5
Common Notation
11Fig. 15.6
12Singleton Pattern
- http//en.wikipedia.org/wiki/Singleton_pattern
public class Singleton // Private
constructor suppresses // generation of a
(public) default constructor private
Singleton() private static class
SingletonHolder private static
Singleton instance new Singleton()
public static Singleton getInstance()
return SingletonHolder.instance
13Fig. 15.7
Messages
also activation bar
asynchronous with dashed lines
14Message Return Values
- Two ways to show the return result
- Using the message syntax
- returnVar message(parameter).
- Using a reply (or return) message line at the end
of an activation bar
15Fig. 15.8
16Fig. 15.9
representing a message sent to the same object
not just to an object of the same class
this.clear()
17Fig. 15.10
New Object Creation
dashed line because UML says so
18Fig. 15.11
Im not sure where this fits in the Java paradigm?
19Fig. 15.12
more items
I think this notation is starting to get out of
hand. I, because I am drawing on the whiteboard,
would draw and arrow like I have.
In general, these are called diagram frames
20Fig. 15.13
I have no easy alternative to this except to not
bother trying to describe an entire algorithm.
21Diagram Frames
Frame Operator
Meaning
Alt Alternative fragment for mutual exclusion conditional logic expressed in the guards.
Loop Loop fragment while guard is true. Can also write loop(n) to indicate looping n times. There is discussion that the specification will be enhanced to define a FOR loop, such as loop(i, 1, 10)
Opt Optional fragment that executes if guard is true.
Par Parallel fragments that execute in parallel.
Region Critical region within which only one thread can run.
22Fig. 15.14
I like this better
23Fig. 15.15
24Fig. 15.16
25Sample Code
public class Sale private
ListltSalesLineItemgt lineItems new
ArrayListltSalesLineItemgt() public Money
getTotal() Money total new Money()
Money subtotal null for (
SalesLineItem lineItem lineItems )
subtotal lineItem.getSubtotal()
total.add( subtotal ) return total
//
26Fig. 15.17
An alternative for Figure 15.16 nothing is
official.
27Fig. 15.18
nested loops
28Fig. 15.19
sd sequence diagram
simplifies the first diagram
29Fig. 15.20
30Code Example
public class Foo public void doX()
// static method call on class Calendar
Locale locales Calendar.getAvailableLocales()
// //
31Fig. 15.21
abstract method call
individual implementations
32Fig. 15.22
asynchronous call
Guideline This arrow difference is subtle. And
when wall sketching UML, it is common to use a
stick arrow to mean a synchronous call because
it's easier to draw. Therefore, when reading a
UML interaction diagram don't assume the shape
of the arrow is correct!
33Code Example
public class ClockStarter public void
startClock() Thread t new Thread( new
Clock() ) t.start() //
asynchronous call to the 'run' method on the
Clock System.runFinalization()
// example follow-on message //
// objects should implement the Runnable
interface // in Java to be used on new threads
public class Clock implements Runnable
public void run() while ( true ) //
loop forever on own thread
// //
34Fig. 15.23
35Fig. 15.24
36Fig. 15.25
37Fig. 15.26
38Fig. 15.27
39Fig. 15.28
40Fig. 15.29
41Fig. 15.30
42Fig. 15.31
43Fig. 15.32
44Fig. 15.33
45Fig. 15.34
46Fig. 15.35