chapter 4: uml interaction diagrams. objective provide a reference for frequently used uml...

45
Chapter 4: Chapter 4: UML Interaction UML Interaction Diagrams Diagrams

Upload: rodger-cain

Post on 03-Jan-2016

249 views

Category:

Documents


4 download

TRANSCRIPT

Chapter 4:Chapter 4: UML Interaction Diagrams UML Interaction Diagrams

ObjectiveObjective

Provide a reference for frequently used Provide a reference for frequently used UML interaction diagram notation-UML interaction diagram notation-sequence and communication diagrams.sequence and communication diagrams.

4.1. Sequence and Communication Diagrams4.1. Sequence and Communication Diagrams

Interaction diagram includesInteraction diagram includes– sequence diagramssequence diagrams– communication diagramscommunication diagramsSequence diagrams Sequence diagrams – illustrate interactions in a kind of fence format illustrate interactions in a kind of fence format

each new object is added to the right each new object is added to the right

Communication diagrams Communication diagrams – illustrate object interactions in a graph or illustrate object interactions in a graph or

network format, network format, objects can be placed anywhere on the diagram objects can be placed anywhere on the diagram

4.1. Sequence and Communication Diagrams4.1. Sequence and Communication Diagrams

public class A { public class A {

private B myB = new B(); private B myB = new B();

public void doOne() { public void doOne() {

myB.doTwo(); myB.doTwo();

myB.doThree(); myB.doThree();

} }

// … // …

} } : A myB : B

doTwo

doOne

doThree

4.1. Sequence and Communication Diagrams4.1. Sequence and Communication Diagrams

: A

myB : B

1: doTwo

2: doThree

doOne

4.1. Sequence and Communication Diagrams4.1. Sequence and Communication Diagrams

Sequence vs. Communication Diagrams? Sequence vs. Communication Diagrams? TypeType StrengthsStrengths WeaknessesWeaknesses

sequencesequence clearly shows sequence or clearly shows sequence or time ordering of messagestime ordering of messages

large set of detailed notation large set of detailed notation optionsoptions

forced to extend to the forced to extend to the right when adding new right when adding new objects; consumesobjects; consumes

communicationcommunication space economicalspace economical

flexibility to add new objects flexibility to add new objects in two dimensionsin two dimensions

more difficult to see more difficult to see sequence of messagessequence of messages

fewer notation optionsfewer notation options

4.1. Sequence and Communication Diagrams4.1. Sequence and Communication Diagrams

Example Sequence Diagram: makePayment Example Sequence Diagram: makePayment

: Register : Sale

makePayment(cashTendered)

makePayment(cashTendered)

: Paymentcreate(cashTendered)

public class Sale { private Payment payment; public void makePayment( Money cashTendered ) {

payment = new Payment( cashTendered ); //…

} // …

}

4.1. Sequence and Communication Diagrams4.1. Sequence and Communication Diagrams

Example Communication Diagram: makePayment Example Communication Diagram: makePayment

1: makePayment(cashTendered)

1.1: create(cashTendered)

:Register :Sale

:Payment

makePayment(cashTendered)

direction of message

4.2. Novice UML Modelers Don't Pay Enough 4.2. Novice UML Modelers Don't Pay Enough Attention to Interaction Diagrams! Attention to Interaction Diagrams!

Spend time doing dynamic object Spend time doing dynamic object modeling, not just static object modeling.modeling, not just static object modeling.– think through the concrete details of what think through the concrete details of what

messages to send, and to whom, and in what messages to send, and to whom, and in what order, order,

– that the "rubber hits the road" in terms of that the "rubber hits the road" in terms of thinking through the true OO design details. thinking through the true OO design details.

4.3. Common UML Interaction Diagram Notation4.3. Common UML Interaction Diagram Notation

Illustrating Participants with Lifeline Boxes Illustrating Participants with Lifeline Boxes

sales: ArrayList<Sale>

:Sale s1 : Sale

lifeline box representing an instance of an ArrayList class, parameterized (templatized) to hold Sale objects

lifeline box representing an unnamed instance of class Sale

lifeline box representing a named instance

sales[ i ] : Sale

lifeline box representing one instance of class Sale, selected from the salesArrayList <Sale> collection

x : List

«metaclass»Font

lifeline box representing the class Font, or more precisely, that Font is an instance of class Class – an instance of a metaclass

related example

List is an interface

in UML 1.x we could not use an interface here, but in UML 2, this (or an abstract class) is legal

4.3. Common UML Interaction Diagram Notation4.3. Common UML Interaction Diagram Notation

Basic Message Expression Syntax Basic Message Expression Syntax return = message(parameter : parameterType) : returnTypereturn = message(parameter : parameterType) : returnType

– ExamplesExamplesinitialize(code) initialize(code)

initialize initialize

d = getProductDescription(id) d = getProductDescription(id)

d = getProductDescription(id:ItemID)d = getProductDescription(id:ItemID)

d = getProductDescription(id:ItemID) : ProductDescriptiond = getProductDescription(id:ItemID) : ProductDescription

4.3. Common UML Interaction Diagram Notation4.3. Common UML Interaction Diagram Notation

Singleton ObjectsSingleton Objects– only one instance of a class instantiatedonly one instance of a class instantiated

: Register1

: Store

doAdoX

the ‘1’ implies this is a Singleton, and accessed via the Singleton pattern

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Lifeline Boxes and Lifelines Lifeline Boxes and Lifelines : Register : Sale

doA

doB

doX

doC

doD

typical sychronous message shown with a filled-arrow line

a found message whose sender will not be specified

execution specification bar indicates focus of control

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Illustrating Reply or Returns Illustrating Reply or Returns

: Register : Sale

d1 = getDate

getDate

doX

aDate

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Messages to "self" or "this" Messages to "self" or "this"

: Register

doX

clear

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Creation of Instances Creation of Instances

: Register : Sale

makePayment(cashTendered)

: Paymentcreate(cashTendered)

authorize

note that newly created objects are placed at their creation "height"

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Object Lifelines and Object Destruction Object Lifelines and Object Destruction

: Sale

: Paymentcreate(cashTendered)

...the «destroy» stereotyped message, with the large X and short lifeline indicates explicit object destruction

«destroy» X

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Diagram Frames in UML Sequence DiagramsDiagram Frames in UML Sequence Diagrams– Looping Looping

enterItem(itemID, quantity)

: B

endSale

a UML loop frame, with a boolean guard expression description, total

makeNewSale

[ more items ]loop

: A

enterItem(itemID, quantity)

: B

endSale

a UML loop frame, with a boolean guard expression description, total

makeNewSale

[ more items ]loop

: A

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Frame Frame OperatorOperator

MeaningMeaning

altalt Alternative fragment for mutual exclusion conditional Alternative fragment for mutual exclusion conditional logic expressed in the guards.logic expressed in the guards.

looploop Loop fragment while guard is true. Can also write loopLoop fragment while guard is true. Can also write loop(n) to indicate looping n times. There is discussion that t(n) to indicate looping n times. There is discussion that the specification will be enhanced to define a FOR loop, he specification will be enhanced to define a FOR loop, such as loop(i, 1, 10)such as loop(i, 1, 10)

optopt Optional fragment that executes if guard is true.Optional fragment that executes if guard is true.

regionregion Critical region within which only one thread can run.Critical region within which only one thread can run.

parpar Parallel fragments that execute in parallel.Parallel fragments that execute in parallel.

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

– Conditional Messages Conditional Messages

calculate

: Bar

yy

xx

[ color = red ]opt

: Foo

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

– Mutually Exclusive Conditional Messages Mutually Exclusive Conditional Messages

: B: A

calculate

doX

: C

calculate

[ x < 10 ]alt

[ else ]

: B: A

calculate

doX

: C

calculate

[ x < 10 ]alt

[ else ]

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

– Iteration Over a Collection Iteration Over a Collection

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 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

i++

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

st = getSubtotal

lineItems[i] :SalesLineItem

t = getTotal

loop

: Sale

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

public class Sale { public class Sale { private List<SalesLineItem> lineItems =private List<SalesLineItem> lineItems =

new ArrayList<SalesLineItem>();new ArrayList<SalesLineItem>();

public Money getTotal() { public Money getTotal() { Money total = new Money(); Money total = new Money(); Money subtotal = null;Money subtotal = null;for ( SalesLineItem lineItem : lineItems ){ for ( SalesLineItem lineItem : lineItems ){

subtotal = lineItem.getSubtotal();subtotal = lineItem.getSubtotal(); total.add( subtotal );total.add( subtotal );

}} return total;return total;

} } // … // …

} }

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

– Nesting of Frames Nesting of Frames

calculate

: Bar

xx

[ color = red ]opt

: Foo

loop(n)

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

How to Relate Interaction Diagrams? How to Relate Interaction Diagrams?

interaction occurrence

note it covers a set of lifelines

note that the sd frame it relates to has the same lifelines: B and C

doA

: A : B : C

doB

sd AuthenticateUser

refAuthenticateUserauthenticate(id)

doX

doM1

: B : C

authenticate(id)

doM2

refDoFoo

sd DoFoo

doX

: B : C

doY

doZ

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram NotationMessages to Classes to Invoke Static MethodsMessages to Classes to Invoke Static Methods

1: locs = getAvailableLocales: Foo

«metaclass»Calendar

doX

message to class, or a static method call

public class Foo { public void doX() { // static method call on class Calendar Locale[] locales = Calendar.getAvailableLocales(); // … } // … }

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Polymorphic Messages and Cases Polymorphic Messages and Cases

:Register

authorize

doX

:Payment {abstract}

polymorphic messageobject in role of abstract superclass

:DebitPayment

doA

authorize

:Foo

stop at this point – don’t show any further details for this message

doB

:CreditPayment

doX

authorize

:Bar

Payment {abstract}

authorize() {abstract}...

CreditPayment

authorize()...

DebitPayment

authorize()...

Payment is an abstract superclass, with concrete subclasses that implement the polymorphic authorize operation

separate diagrams for each polymorphic concrete case

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notation

Asynchronous and Synchronous Calls Asynchronous and Synchronous Calls

:ClockStarter

:Clock

run

startClock

create

a stick arrow in UML implies an asynchronous call

a filled arrow is the more common synchronous call

In Java, for example, an asynchronous call may occur as follows:

// Clock implements the Runnable interfaceThread t = new Thread( new Clock() );t.start();

the asynchronous start call always invokes the run method on the Runnable (Clock) object

to simplify the UML diagram, the Thread object and the start message may be avoided (they are standard “overhead”); instead, the essential detail of the Clock creation and the run message imply the asynchronous call

runFinalization

System : Class

active object

4.4. Basic Sequence Diagram Notation4.4. Basic Sequence Diagram Notationpublic class ClockStarter { public class ClockStarter {

public void startClock() { public void startClock() { Thread t = new Thread( new Clock() );Thread t = new Thread( new Clock() ); // asynchronous call to the 'run' method on the Clock// asynchronous call to the 'run' method on the Clock t.start();t.start(); // example follow-on message// example follow-on messageSystem.runFinalization(); System.runFinalization(); } } // … // …

} } // objects should implement the Runnable interface // objects should implement the Runnable interface // in Java to be used on new threads // in Java to be used on new threads public class Clock implements Runnable { public class Clock implements Runnable {

public void run() { public void run() { while ( true ) {// loop forever on own thread while ( true ) {// loop forever on own thread

// … // … } }

}} // … // …

} }

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

LinksLinks

1: makePayment(cashTendered)2: foo

2.1: bar: Register :Sale

link line

All messages flow on the same line

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Messages Messages

1: msg22: msg33: msg4

3.1: msg5: Register :Sale

all messages flow on the same link

msg1

Don't number the starting message.

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Messages to "self" or "this" Messages to "self" or "this"

: Register

msg1

1: clear

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Creation of Instances Creation of Instances

1: create(cashier)

: Register :Sale

create message, with optional initializing parameters. This will normally be interpreted as a constructor call.

«create»1: make(cashier)

: Register :Sale

if an unobvious creation message name is used, the message may be stereotyped for clarity

1: create(cashier)

: Register :Sale {new}

Three ways to show creation in a communication diagram

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Message Number SequencingMessage Number Sequencing– The first message is not numbered. The first message is not numbered. – The order and nesting of subsequent The order and nesting of subsequent

messages is shown with a legal numbering messages is shown with a legal numbering scheme in which nested messages have a scheme in which nested messages have a number appended to them. number appended to them.

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

: Amsg1 : B1: msg2

: C

1.1: msg3not numbered

legal numbering

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

: Amsg1 : B1: msg2

: C

1.1: msg3

2.1: msg5

2: msg4

: D

2.2: msg6

first second

fourth

sixth

fifth

third

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Conditional Messages Conditional Messages

1 [ color = red ] : calculate: Foo : Bar

message1

conditional message, with test

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Mutually Exclusive Conditional Paths Mutually Exclusive Conditional Paths

1a [test1] : msg2

: A : B

: C

1a.1: msg3

msg1

: D

1b [not test1] : msg4

1b.1: msg5

: E

2: msg6

unconditional after either msg2 or msg4 1a and 1b are mutually

exclusive conditional paths

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Iteration or Looping Iteration or Looping

1 * [ i = 1..n ]: num = nextInt: SimulatorrunSimulation : Random

iteration is indicated with a * and an optional iteration clause following the sequence number

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Iteration Over a Collection Iteration Over a Collection 1 * [i = 1..n]: st = getSubtotal: Salet = 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 SalesLineItems; the ‘i” value comes from the message clause.

lineItems[i]:SalesLineItem

this iteration and recurrence clause indicates we are looping across each element of the lineItems collection.

1 *: st = getSubtotal: Salet = getTotal lineItems[i]:SalesLineItem

Less precise, but usually good enough to imply iteration across the collection members

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Messages to a Classes to Invoke Static Messages to a Classes to Invoke Static Methods Methods

1: locs = getAvailableLocales: Foo

«metaclass»Calendar

doX

message to class, or a static method call

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Polymorphic Messages and Cases Polymorphic Messages and Cases

:Register authorizedoX :Payment {abstract}

polymorphic message

object in role of abstract superclass

:DebitPayment

authorize

:Foo

stop at this point – don’t show any further details for this message

separate diagrams for each polymorphic concrete case

doAdoB :CreditPayment

authorize

:BardoX

4.5. Basic Communication Diagram Notation4.5. Basic Communication Diagram Notation

Asynchronous and Synchronous Calls Asynchronous and Synchronous Calls

3: runFinalization:ClockStarter System : Class

startClock

:Clock

1: create

2: run

asynchronous message

active object

Questions & AnswersQuestions & Answers