chapter 15 uml interaction diagrams 1cs6359 fall 2012 john cole

38
Chapter 15 UML Interaction Diagrams 1 CS6359 Fall 2012 John Cole

Upload: demarcus-thorne

Post on 01-Apr-2015

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Chapter 15

UML Interaction Diagrams

1CS6359 Fall 2012 John Cole

Page 2: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

What are Interaction Diagrams?

• Illustrate how objects interact through messages

• Two types: Sequence Diagrams and Communication Diagrams

2CS6359 Fall 2012 John Cole

Page 3: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Example Code

Public class A

{

Private BB myB = new BB();

Public void doOne()

{

myB.doTwo();

myB.doThree();

}

}3CS6359 Fall 2012 John Cole

Page 4: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Sequence Diagram

A B

4CS6359 Fall 2012 John Cole

Page 5: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Sequence Diagram

: Register : Sale

makePayment(cashTendered)

makePayment(cashTendered)

: Paymentcreate(cashTendered)

an activation box showingthe focus of control

5CS6359 Fall 2012 John Cole

Page 6: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Guideline

• Spend time doing dynamic modeling with interaction diagrams, not just static object modeling with class diagrams

• This makes you think about how things happen, not just the objects you need

6CS6359 Fall 2012 John Cole

Page 7: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Sequence vs. Communication

• UML tools usually emphasize sequence• Easier to see call flow sequence—read from

top to bottom• Communication diagrams are more space-

efficient • Communication Diagrams expand top to

bottom; Sequence Diagrams expand left to right

7CS6359 Fall 2012 John Cole

Page 8: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Common Notation

CS6359 Fall 2012 John Cole 8

Page 9: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Singleton Objects

• Only one instance of the class instantiated at any time.

CS6359 Fall 2012 John Cole 9

Page 10: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Lifeline Boxes

• Lifeline is a dashed (or solid, in UML 2) line below the object.

10CS6359 Fall 2012 John Cole

Page 11: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Messages

CS6359 Fall 2012 John Cole 11

Page 12: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Focus of Control

• The bar on the left is an Execution Specification bar. Indicates focus of control

CS6359 Fall 2012 John Cole 12

Page 13: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Reply or Returns

• Use the message syntax returnVar=Message(parameter)

• Using a reply or return message at the end of an activation bar

CS6359 Fall 2012 John Cole 13

Page 14: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Messages to Self

• Message sent to itself with a nested activation bar

CS6359 Fall 2012 John Cole 14

Page 15: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Creation of Instances

• Dashed line with filled arrow for synchronous message, open if async. Name “create” is not required; it’s a UML idiom

CS6359 Fall 2012 John Cole 15

Page 16: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Lifelines and Object Destruction

• Show explicit destruction of an object, such a when a database connection is closed

CS6359 Fall 2012 John Cole 16

Page 17: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Diagram Frames

• These support conditionals and looping

CS6359 Fall 2012 John Cole 17

Page 18: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Frame OperatorsFrame 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. Can be enhanced to define for loop

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

A “guard” is a Boolean test in brackets over the line to which it belongs

CS6359 Fall 2012 John Cole 18

Page 19: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Conditional Messages

CS6359 Fall 2012 John Cole 19

Page 20: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

UML 1.x Conditionals

CS6359 Fall 2012 John Cole 20

Page 21: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Conditional If…Else

CS6359 Fall 2012 John Cole 21

Page 22: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Code to Iterate Over Collectionpublic class Sale { private List<SalesLineItem> lineItems = new

ArrayList<SalesLineItem>(); public Money getTotal() { Money total = new Money(); Money subtotal = null; for ( SalesLineItem lineItem : lineItems ) { subtotal = lineItem.getSubtotal(); total.add( subtotal ); } return total; } // . . }

CS6359 Fall 2012 John Cole 22

Page 23: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Implicit Iteration

CS6359 Fall 2012 John Cole 23

Page 24: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Nesting Frames

CS6359 Fall 2012 John Cole 24

Page 25: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Relating Interaction Diagrams• Interaction Occurrence (or Use) is a reference

to an interaction within another interaction.

CS6359 Fall 2012 John Cole 25

Page 26: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Invoking Static Methods

• Metaclasses or static classes are instances of themselves. Calling a static method uses the name of the class, not the name of the instance.

CS6359 Fall 2012 John Cole 26

Page 27: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Polymorphic Messages

• Multiple sequence diagrams: One to the superclass, one to each case, each starting with a found polymorphic message (next slide)

CS6359 Fall 2012 John Cole 27

Page 28: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Polymorphic Messages

CS6359 Fall 2012 John Cole 28

Page 29: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Async and Sync Calls

• Async (non-blocking) calls are a stick (unfilled) arrow, sync are a filled arrow

• This can be used for multi-threadingPublic class ClockStarter {

Public void startClock {

Thread t = new Thread(new Clock));

t.start();

System.runFinalization(); //

}

}

CS6359 Fall 2012 John Cole 29

Page 30: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Async Calls

CS6359 Fall 2012 John Cole 30

Page 31: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Links

• Connection path between objects. Navigation and visibility. Instance of an association.

• Multiple messages and messages both ways

CS6359 Fall 2012 John Cole 31

Page 32: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Creation of Instances

• Message named Create. If some other name is used, use the UML stereotype <<Create>> to indicate. Message can include parameters.

• UML tagged value [new] to indicate creation

CS6359 Fall 2012 John Cole 32

Page 33: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Message Number Sequencing

• If ordering is important, indicate this with sequence numbers.

• The first message is not numbered• Order and nesting of subsequent numbers is

shown with levels (next slide)

CS6359 Fall 2012 John Cole 33

Page 34: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Sequencing

CS6359 Fall 2012 John Cole 34

Page 35: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Conditional Messages

• Show this with the condition in brackets

CS6359 Fall 2012 John Cole 35

Page 36: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Mutually Exclusive Conditionals

• Equivalent of If…else. Modify the sequence expression with a letter:

CS6359 Fall 2012 John Cole 36

Page 37: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Iteration

CS6359 Fall 2012 John Cole 37

Page 38: Chapter 15 UML Interaction Diagrams 1CS6359 Fall 2012 John Cole

Iteration Over a Collection

CS6359 Fall 2012 John Cole 38