1 sequence diagrams (based on stevens and pooley (2006, chapters 9, 10) and fowler (2004, chapter...

12
1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

Upload: drusilla-skinner

Post on 01-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

1

Sequence Diagrams(Based on

Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4))

David MeredithAalborg University

Page 2: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

2

Interaction diagrams

• Use case diagram describes tasks that system must help actors to perform

• Class diagram describes classes required to realize use cases and relationships between these classes

• Interaction diagrams describe how objects interact to realize the use case scenarios

Page 3: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

3

Sequence diagrams

ActorA B C

sync msg 1

sync msg 2

return value

sync msg 3

sync msg to self

Activation, VLifeline

Participants

Response, return Activation, X

• Sequence diagram shows how actors and objects interact to realize a use case scenario

• Only shows actors and objects involved in the scenario

• Each object or actor is called a participant and is represented by an icon in a row across the top of the diagram

• Extending down the page from each participant is a dashed line called a lifeline

• Time is understood to move forward as we move down the diagram

• A message sent from participant A to participant B is represented by an arrow with a solid line drawn from the lifeline of A to the lifeline of B (sync msg 2)

• If A has to stop computing while B carries out the operation invoked by the message sent to it by A, then this message is said to be synchronous and control is passed from A to B

• A synchronous message is represented on a sequence diagram by an arrow with a solid black triangular head

Page 4: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

Sequence diagrams• When B receives the message, it

starts to have a new live activation, denoted here by V

• This activation is represented by drawing a narrow rectangle covering the lifeline of B, starting at the point where the arrow representing the message from A hits the lifeline of B

• In a procedural interaction (no concurrency), exactly one object is computing at any given instant

• If B sends a message to another object, C, B may stop computing temporarily while C starts to have a new live activation, X

• B will continue to have the live activation, V, until it has finished carrying out the operation invoked by the message, sync msg 2, sent to it by A

• At the end of its activation, B might send a return value to A, indicated by the dashed arrow with the stick head shown

4

ActorA B C

sync msg 1

sync msg 2

return value

sync msg 3

sync msg to self

Activation, VLifeline

Participants

Response, return Activation, X

Page 5: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

5

Sequence diagrams

ActorA B C

sync msg 1

sync msg 2

return value

sync msg 3

sync msg to self

Activation, VLifeline

Participants

Response, return Activation, X

• Can indicate when an object is computing by shading those periods on the activations

• For readability, the participants should generally be arranged in the order in which they first participate in the interaction

• This means that most of the message sends are represented by arrows that go from left to right

• If an object of class A sends a message to an object of class B in a sequence diagram, this implies that there is an association between class A and class B in the class model

• In procedural systems, only actors can initiate activity by sending a message "out of the blue"

• System objects can only send messages when they have been made active by receiving a message from another object

Page 6: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

Sequence diagrams

ActorA B C

sync msg 1

sync msg 2

return value

sync msg 3

sync msg to self

Activation, VLifeline

Participants

Response, return Activation, X

• At any given instant there will be a stack of live activations and the one at the top of the stack will be the one that currently has control and is computing

• All the other activated objects are waiting to receive control back from objects to which they have sent messages

• If the object whose activation is currently at the top of the stack (e.g., B) sends a message, then the object that receives this message (C) becomes active and its activation (X) becomes the top of the stack

• If the activation X of the object, C, is at the top of the stack and it terminates, then its activation is removed from the stack and control returns to the object B whose message send, sync msg 3, started the activation X

• The activation V during which sync msg 3 was sent then becomes the new top of the stack 6

Page 7: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

7

Sequence diagrams

ActorA B C

sync msg 1

sync msg 2

return value

sync msg 3

sync msg to self

Activation, VLifeline

Participants

Response, return Activation, X

• If an object sends a message to itself, then the object gets another new live activation

• That is, the object now has two live activations on the stack

• Represented on sequence diagram by a nested activation, with activation resulting from message to self slightly offset from older activation

Page 8: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

More advanced features

: PersonnelOfficer

: Lecturer

: HeadOfDepartmentnew HeadOfDepartment(n)

getName()

n = getName() : "David Meredith"

destroy()

• Diagram describes process of promoting me to be Head of Department within personnel department database

• No need to name participant objects and actors

– Can just precede class of object with a colon to indicate that participant is an object, not a class

• Reply message arrow can be labelled with assignment of return value to a variable

– Actual value returned can be given after colon

• Object disposal or destruction indicated by a synchronous message sent to the object whose lifeline is terminated with an X

• Construction of new object indicated by horizontal asynchronous message arrow directly to the head of the new participant

– Asynchronous message arrow has stick head

• Synchronous message arrow has solid black triangular head

– Asynchronous message starts a new thread of control

• Participant that sends asynchronous message continues computing after sending message

Page 9: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

9

Conditional behaviour: BookBorrower

theLibraryMember:LibraryMember

theCopy:Copy

theBook:Book

borrow(theCopy)

okToBorrow()

beBorrowed(theLibraryMember)

copyBorrowed(theCopy)

canBorrow = okToBorrow()

opt [canBorrow == true]

• Different scenarios within a use case can sometimes be represented on same sequence diagram by guarding parts of the interaction with conditions

• Conditional behaviour enclosed within rectangle labelled “opt” (for “optional”)

• Guard condition written between square brackets near “opt” label

Page 10: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

10

Conditional behaviour and loopsloop and alt frames

:Ordercareful:

Distributorordinary:

Distributor

dispatch

loop [for each line item]

alt [value > £10000]

dispatch

[else]

dispatch

Found message No object name

Asynchronous message

for each line item if value > £10000 careful.dispatch else ordinary.dispatch

Guards (must bemutually exclusive)

Loop operatorfor iteration

alt operator for if -else

Sequence diagrams not goodat expressing looping andconditionals - very clumsy notation.

Better to express complexlooping and conditionals withpseudocode.

Page 11: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

11

Concurrency and asynchronous calls

• In single-threaded, procedural interaction, only one participant computing at any given time and all messages are synchronous– When object sends synchronous message, stops computing and loses

control until receiver finishes handling message and returns– Return may be indicated on diagram

• In multi-threaded, concurrent interaction, two or more objects computing simultaneously– Some messages may be asynchronous

• When object sends asynchronous message, can continue computing and recipient also gets a live activation – generates two concurrent threads

:StudentActor:Student :PersonalTutor

:PersonalTutorActor

chooseCourses(m1,...m6)

confirmChoice(m1,...m6,self)

sendEMail

messageSynchronous message or call

messageAsynchronous message

Return from earlier message -unblocks synchronous messagesend

Page 12: 1 Sequence Diagrams (Based on Stevens and Pooley (2006, Chapters 9, 10) and Fowler (2004, Chapter 4)) David Meredith Aalborg University

12

Summary• Interaction diagrams describe how objects

interact to realize use cases• Sequence diagrams

– participants, lifelines, activations, messages, returns

– synchronous and asynchronous messages– messages to self– representing creation and destruction of

objects– representing return values– representing conditionals and loops– asynchronous calls and concurrency