42
TK2023 Object- Oriented Software Engineering CHAPTER 10 INTERACTION DIAGRAMS

10 Interaction Diagrams

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 10 Interaction Diagrams

TK2023 Object-Oriented Software Engineering

CHAPTER 10

INTERACTION DIAGRAMS

Page 2: 10 Interaction Diagrams

INTRODUCTION

UML provides interaction diagrams for illustrating how objects interact via messages.

Interaction diagrams are used for dynamic object modelling.

There are two common types: sequence diagram communication diagram

Page 3: 10 Interaction Diagrams

Sequence diagrams show interactions in a fence format.Example:

class AddressBookApp {private AddressBook addrBook;…public void addContact() {

addrBook.addNewContact();addrBook.reset();

}}…

AddressBookApp app = new AddressBookApp();…app.addContact();

: AddressBookApp : AddressBook

addContact()addNewContact

reset

Page 4: 10 Interaction Diagrams

Communication diagrams show interactions in a graph format.Example:

class AddressBookApp {private AddressBook addrBook;…public void addContact() {

addrBook.addNewContact();addrBook.reset();

}}…

AddressBookApp app;app = new AddressBookApp();…app.addContact();

: AddressBookApp

addrBook : AddressBook

addContact()

1: addNewContact

2: reset

Page 5: 10 Interaction Diagrams

SEQUENCE DIAGRAM VS COMMUNICATION DIAGRAMType Strengths Weaknesses

sequence Clearly shows sequence or time ordering of messages

Large set of detailed notation options

Forced to extend to the right when adding new objects; consumes horizontal space

communication Space economical - flexibility to add new objects in two dimensions

More difficult to see sequence of messages

Fewer notation options

Page 6: 10 Interaction Diagrams

LIFELINE BOXES

Informally, lifeline boxes represent the participants in an interaction.

:Sale

lifeline box representing an unnamed instance of class Sale

Page 7: 10 Interaction Diagrams

s1 : Sale lifeline box representing a named instance of class Sale

sales : ArrayList<Sale>

sales[ i ] : Sale

lifeline box representing an instance of an ArrayList class, parameterized to hold Sale objectslifeline box representing one instance of class Sale, selected from the sales collection.

Page 8: 10 Interaction Diagrams

BASIC MESSAGE EXPRESSION SYNTAX

: Register : Store

doA

message send

Syntax for message expressions:initialize(code)initialized = getProductDescription(id)d = getProductDescription(id:ItemID)d = getProductDescription(id:ItemID) : ProductDescription

Page 9: 10 Interaction Diagrams

BASIC SEQUENCE DIAGRAM NOTATION A (synchronous) message between objects is

represented with a message expression on a filled-arrowed solid line between the vertical lifelines. The time ordering is from top to bottom.

Page 10: 10 Interaction Diagrams

: 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

Page 11: 10 Interaction Diagrams

There are two ways to show the return result from a message:

: Register : Sale

d1 = getDate

getDate

doX

aDate

Page 12: 10 Interaction Diagrams

A message being sent from an object to itself is shown using a nested activation bar.

: Register

doX

clear

Page 13: 10 Interaction Diagrams

Object creation notation is shown below:

: Register : Sale

makePayment(cashTendered)

: Paymentcreate(cashTendered)

authorize

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

Page 14: 10 Interaction Diagrams

The following is also permissable:

: Register : Sale

makePayment(cashTendered)

: PaymentPayment(cashTendered)

authorize

Page 15: 10 Interaction Diagrams

Looping constructs are supported in UML via frames. Frames have an operator and a guard.

a UML loop frame , with a boolean guard expression

enterItem(itemID, quantity)

: B

endSale

description , total

makeNewSale

[ more items ]loop

: A

Page 16: 10 Interaction Diagrams

Conditional constructs are represented as OPT frames.

calculate

: Bar

yy

xx

[ color = red ]opt

: Foo

Page 17: 10 Interaction Diagrams

Mutally exclusive conditional messages are represented as ALT frames.

: B: A

calculate

doX

: C

calculate

[ x < 10 ]alt

[ else ]

Page 18: 10 Interaction Diagrams

Representing iteration over a collection:

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

[ i < lineItems.size ]loop

: Sale

i++

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

Page 19: 10 Interaction Diagrams

The following is simpler but excludes certain details:

st = getSubtotal

lineItems[i] :SalesLineItem

t = getTotal

loop

: Sale

Page 20: 10 Interaction Diagrams

Frames can be nested as shown below:

calculate

: Bar

xx

[ color = red ]opt

: Foo

loop(n)

Page 21: 10 Interaction Diagrams

An interaction diagram can contain references to other interaction diagrams. These references are called interaction occurrences.

Page 22: 10 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

Page 23: 10 Interaction Diagrams

How to show polymorphism in a sequence diagram?

One approach is to use multiple sequence diagrams as shown in the following example.

Page 24: 10 Interaction Diagrams

:Register

authorize

doX

:Payment

:DebitPayment

doA

authorize

:Foo

doB

:CreditPayment

doX

authorize

:Bar

Payment

authorize() ...

CreditPayment

authorize()...

DebitPayment

authorize()...

Page 25: 10 Interaction Diagrams

BASIC COMMUNICATION DIAGRAM NOTATION A link is a connection path between two

objects; it indicates some form of navigation and visibility between the objects is possible.

More formally, a link is an instance of an association.

1: makePayment (cashTendered)2: foo

: Register :Sale

Page 26: 10 Interaction Diagrams

Each message between objects is represented with a message expression and a small arrow indicating the direction of the message.

A sequence number is added to show the sequential order of messages.

It is advisable not to number the starting message to simplify the overall numbering.

1: msg22: msg33: msg4

: Register :Sale

msg1

Page 27: 10 Interaction Diagrams

A message sent from an object to itself is shown below:

: Register

msg1

1: clear

Page 28: 10 Interaction Diagrams

Any message can be used to create an instance. The convention in the UML is to use a message named create.

If another message name is used, the message may annotated with the «create» stereotype.

1: create(cashier)

: Register :Sale

«create»1: Sale(cashier)

: Register :Sale

Page 29: 10 Interaction Diagrams

1: create(cashier)

: Register :Sale {new}

The UML tagged value {new} may optionally be added to the lifeline box to highlight the creation.

Page 30: 10 Interaction Diagrams

The order of messages is illustrated with sequence numbers. The first message is not numbered. The order and nesting of subsequent messages is

shown with a nested numbering scheme.

: Amsg1

: B1: msg2

: C

1.1: msg3

Page 31: 10 Interaction Diagrams

: Amsg1

: B1: msg2

: C

1.1: msg3

2.1: msg5

2: msg4

: D

2.2: msg6

Page 32: 10 Interaction Diagrams

: Amsg1

: B1: msg2

: C2: msg4

: D

: C

1.1: msg3

2.1: msg5

2.2: msg6

Page 33: 10 Interaction Diagrams

: A : B1: msg2

: C

1.1: msg3

: D

2.2: msg6

2.1: msg5

2: msg4

msg1

Page 34: 10 Interaction Diagrams

: A : : B1: msg2

: C

1.1: msg3

: D

2.2: msg6

2.1: msg5

2: msg4

msg1

Page 35: 10 Interaction Diagrams

: A : B

: C

2.1: msg5

2: msg4

: D

2.2: msg6

msg1 1: msg2

1.1: msg3

Page 36: 10 Interaction Diagrams

: A : B

: C

2.1: msg5

2: msg4

: D

2.2: msg6

msg1 1: msg2

1.1: msg3

Page 37: 10 Interaction Diagrams

: A : B

: C

2.1: msg5

2: msg4

: D

2.2: msg6

msg1 1: msg2

1.1: msg3

Page 38: 10 Interaction Diagrams

A conditional message is shown by following a sequence number with a conditional clause in square brackets. The message is sent only if the clause evaluates to true.

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

message1

Page 39: 10 Interaction Diagrams

The following diagram is an example with mutually exclusive conditional paths.

1a [color = red] : msg2

: A : B

: C

1a.1: msg3

msg1

: D

1b [not (color = red)] : msg4

1b.1: msg5

: E

2: msg6

Page 40: 10 Interaction Diagrams

Iteration notation is shown below.

If the details of the iteration clause are not important to the modeller, a simple * can be used.

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

1 * : num = nextInt: SimulatorrunSimulation : Random

Page 41: 10 Interaction Diagrams

The following shows how to represent iteration over a collection (such as an array or a Vector), sending the same message to each.

1 * [i = 1..n]: st = getSubtotal: Sale

t = getTotal lineItems[i]:SalesLineItem

1 *: st = getSubtotal: Sale

t = getTotal lineItems[i]:SalesLineItem

Page 42: 10 Interaction Diagrams

As with sequence diagrams, multiple communication diagrams can be used to show each concrete polymorphic case.

:RegisterauthorizedoX

:Payment {abstract}

:DebitPayment :Foo

doAdoB

:CreditPayment :BardoX

authorize authorize