Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

8.1 Interaction Diagrams: Interaction Diagrams Are Used To Model The Dynamic Aspects of A Software System

You are on page 1of 13

8.

1 Interaction Diagrams

Interaction diagrams are used to model the dynamic


Object-Oriented Software Engineering aspects of a software system
Practical Software Development using UML and Java They help you to visualize how the system runs.
An interaction diagram is often built from a use case and
a class diagram.
Chapter 8: —The objective is to show how a set of objects
Modelling Interactions and Behaviour accomplish the required interactions with an actor.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 2

Interactions and messages Elements found in interaction diagrams

Interaction diagrams show how a set of actors and Instances of classes


objects communicate with each other to perform: —Shown as boxes with the class and object identifier
—The steps of a use case, or underlined
—The steps of some other piece of functionality.
Actors
The set of steps, taken together, is called an interaction. —Use the stick-person symbol as in use case diagrams

Interaction diagrams can show several different types of Messages


communication. —Shown as arrows from actor to object, or from object
—E.g. method calls, messages send over the network to object
—These are all referred to as messages.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 3 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 4
Creating instances diagrams Sequence diagrams – an example

You should develop a class diagram and a use case


model before starting to create an interaction diagram. :CourseSection :Student

There are two kinds of interaction diagrams: requestToRegister :Registration


<<create>>
—Sequence diagrams addToSchedule

—Collaboration diagrams addToRegistrationList

Course * CourseSection * Registration * Student


getPrerequisite requestToRegister addToSchedule
addToRegistrationList hasPassedCourse

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 5 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 6

Sequence diagrams –
Sequence diagrams
same example, more details
A sequence diagram shows the sequence of messages exchanged by
the set of objects performing a certain task
The objects are arranged horizontally across the diagram. GUI :CourseSection aStudent: :Course
Student
An actor that initiates the interaction is often shown on the left.
requestToRegister
requestToRegister
The vertical dimension represents time. (aStudent)
prereq :=
hasPrerequisite := getPrerequisite
A vertical line, called a lifeline, is attached to each object or actor. hasPassedCourse(prereq)
[hasPrerequisite]
The lifeline becomes a broad box, called an activation box during <<create>> :Registration

the live activation period. addToSchedule


addToRegistrationList
A message is represented as an arrow between activation boxes of
the sender and receiver.
—A message is labelled and can have an argument list and a
return value.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 7 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 8
Sequence diagrams – Sequence diagrams –
an example with replicated messages an example with object deletion
An iteration over objects is indicated by an asterisk If an object’s life ends, this is shown with an X at the
preceding the message name end of the lifeline

:Bill :Purchase :Item


:SpecificFlight :Booking :PassengerRole

*[for all Purchase] getSubtotal cancelBooking


getUnitPrice cancel
deleteFromItinerary
deleteFromPassengerList
computeTotal

Bill * Purchase * Item


quantity price

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 9 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 10

Collaboration diagrams – an example Collaboration diagrams

Collaboration diagrams emphasise how the objects


collaborate in order to realize an interaction
A collaboration diagram is a graph with the objects as
1: <<create>> 2: addToSchedule the vertices.
:CourseSection :Registration :Student
Communication links are added between objects
3: addToRegistrationList
Messages are attached to these links.
—Shown as arrows labelled with the message name
Time ordering is indicated by prefixing the message with
some numbering scheme.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 11 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 12
Collaboration diagrams –
Communication links
same example, more details
A communication link can exist between two objects
whenever it is possible for one object to send a message
2: prereq := getPrerequisite
to the other one.
1: requestToRegister(aStudent)
<<local>>
GUI :CourseSection :Course

3: hasPrerequisite := 5: addToRegistrationList
Several situations can make this message exchange
hasPassedCourse(prereq) 4: [hasPrerequisite]
<<parameter>> <<create>>
<<parameter>> possible:
aStudent: :Registration
Student 5: addToSchedule
<<parameter>> 1. The classes of the two objects have an association
between them.
- This is the most common case.
- If all messages are sent in the same direction, then probably
the association can be made unidirectional.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 13 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 14

Other communication links Other communication links

2. The receiving object is stored in a local variable of 4. The receiving object is global.
the sending method. - This is the case when a reference to an object can be
- This often happens when the object is created in the sending obtained using a static method.
method or when some computation returns an object . - The stereotype «global», or a [G] symbol is used in this
- The stereotype to be used is «local» or [L]. case.

3. A reference to the receiving object has been received 5. The objects communicate over a network.
as a parameter of the sending method. - We suggest to write «network».
- The stereotype is «parameter» or [P].

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 15 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 16
How to choose between using a sequence How to choose between using a sequence
or collaboration diagram or collaboration diagram
Sequence diagrams Collaboration diagrams
Make explicit the time ordering of the interaction. Can be seen as a projection of the class diagram
—Use cases make time ordering explicit too —Might be preferred when you are deriving an
—So sequence diagrams are a natural choice when you interaction diagram from a class diagram.
build an interaction model from a use case. —Are also useful for validating class diagrams.

Make it easy to add details to messages.


—Collaboration diagrams have less space for this

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 17 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 18

Collaboration diagrams and patterns 8.2 State Diagrams

A collaboration diagram can be used to represent aspects of A state diagram describes the behaviour of a system,
a design pattern some part of a system, or an individual object.
2: [information needed At any given point in time, the system or object is in a
1: request
and not loaded]
loadHeavyWeight
certain state.
:Client :Proxy :HeavyWeight
—Being in a state means that it is will behave in a
a) specific way in response to any events that occur.
proxy heavyWeight
Some events will cause the system to change state.
Student PersistentStudent
—In the new state, the system will behave in a
Proxy
different way to events.
client
A state diagram is a directed graph where the nodes are
CourseSection states and the arcs are transitions.
b)

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 19 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 20
State diagrams – an example States

tic-tac-toe game At any given point in time, the system is in one state.

XWin
It will remain in this state until an event occurs that
XTurn causes it to change state.

Tie
A state is represented by a rounded rectangle containing
the name of the state.

OWin
Special states:
OTurn
—A black circle represents the start state
—A circle with a ring around it represents an end state

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 21 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 22

Transitions State diagrams – an example of transitions


with time-outs and conditions
A transition represents a change of state in response to GreenLightNoTrigger
an event.
vehicleWaitingToTurn
—It is considered to occur instantaneously.
GreenLight GreenLightChangeTriggered

The label on each transition is the event that causes the after(25s)
after(25s since exit from
state RedLight)
change of state.
YellowLight after(30s) YellowLight after(30s)

after(5s) after(5s)

RedLight RedLight

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 23 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 24
State diagrams – an example with
Activities in state diagrams
conditional transitions
An activity is something that takes place while the
system is in a state.
Planned
openRegistration

closeRegistration
—It takes a period of time.
requestToRegister
Cancelled OpenNotEnoughStudents (aStudent)
cancel /createRegistration
classSize >= minimum
—The system may take a transition out of the state in
cancel
response to completion of the activity,
closeRegistration
requestToRegister
Closed OpenEnoughStudents (aStudent)
classSize >= maximum /createRegistration
—Some other outgoing transition may result in:
- The interruption of the activity, and
- An early exit from the state.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 25 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 26

State diagram – an example with activity Actions in state diagrams

An action is something that takes place effectively


instantaneously
press button —When a particular transition is taken,
ProposeSelection MusicPlaying
—Upon entry into a particular state, or
do:
play chosen —Upon exit from a particular state
selection
An action should consume no noticeable amount of time

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 27 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 28
State diagram – an example with actions State diagrams – another example

Closed Opening
Enter / pressButton Enter /
stop motor run motor forwards
Recording endOfTape Rewinding
closingCompleted
Exit / stop
openingCompleted
pressButton
startOfTape / stop

endOfProgram
Closing pressButton Open
Wait
Enter / Enter /
run motor in reverse stop motor

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 29 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 30

Nested substates and guard conditions State diagram – an example with substates

A state diagram can be nested inside a state.


The states of the inner diagram are called substates. Planned

openRegistration
Cancelled Open requestToRegister
selectReverse
Neutral Reverse cancel (aStudent)
do:
selectNeutral NotEnoughStudents /createRegistration
unregister
students closeRegistration
selectFirst selectDrive selectSecond selectNeutral
classSize >= minimum
reachSecondSpeed reachThirdSpeed
[driveSelected] [driveSelected]
First Second Third classSize >= maximum
dropBelowSecondSpeed dropBelowThirdSpeed Closed EnoughStudents
[driveSelected] closeRegistration

selectFirst selectSecond

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 31 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 32
8.3 Activity Diagrams Activity diagrams – an example
Receive course
An activity diagram is like a state diagram. registration request

—Except most transitions are caused by internal events, such as


the completion of a computation.
Check Verify
An activity diagram prerequisites course not
full
[not ok] [ok]
—Can be used to understand the flow of work that an object or
[ok] [not ok]
component performs. Check
special [ok]
—Can also be used to visualize the interrelation and interaction permission
between different use cases.
[not ok]
—Is most often associated with several classes.

One of the strengths of activity diagrams is the representation of Complete


registration
concurrent activities.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 33 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 34

Representing concurrency Representing concurrency

Concurrency is shown using forks, joins and rendezvous. —A join has multiple incoming transitions and one
outgoing transition.
- The outgoing transition will be taken when all incoming
—A fork has one incoming transition and multiple
transitions have occurred.
outgoing transitions.
- The incoming transitions must be triggered in separate
- The execution splits into two concurrent threads. threads.
- If one incoming transition occurs, a wait condition occurs at
—A rendezvous has multiple incoming and multiple the join until the other transitions occur.
outgoing transitions.
- Once all the incoming transitions occur all the outgoing
transitions may occur.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 35 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 36
Activity diagrams – an example with
Swimlanes
swimlanes Student CourseSection

Activity diagrams are most often associated with several


Receive course
classes. registration request

The partition of activities among the existing classes can


be explicitly shown using swimlanes.
Check Verify
prerequisites course not
full
[not ok] [ok]
[ok] [not ok]
Check
special [ok]
permission
[not ok]

Complete
registration

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 37 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 38

8.4 Implementing Classes Based on


Example: The CourseSection class
Interaction and State Diagrams
You should use these diagrams for the parts of your States:
system that you find most complex. ‘Planned’:
—I.e. not for every class
— closedOrCancelled == false && open ==
false
Interaction, activity and state diagrams help you create a
‘Cancelled’:
correct implementation.
— closedOrCancelled == true &&
This is particularly true when behaviour is distributed registrationList.size() == 0
across several use cases. ‘Closed’ (course section is too full, or being taught):
—E.g. a state diagram is useful when different — closedOrCancelled == true &&
conditions cause instances to respond differently to registrationList.size() > 0
the same event.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 39 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 40
Example: The CourseSection class Example: The CourseSection class

States: Class diagram


‘Open’ (accepting registrations):
— open == true
Course * CourseSection * Registration * Student
‘NotEnoughStudents’ (substate of ‘Open’):
getPrerequisite requestToRegister addToSchedule
— open == true && registrationList.size() < addToRegistrationList hasPassedCourse

course.getMinimu m()
‘EnoughStudents’ (substate of ‘Open’):
— open == true && registrationList.size() >=
course.getMinimu m()

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 41 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 42

Example: The CourseSection class Example: The CourseSection class


public class CourseSection
public CourseSection(Course course)
{
{
// The many-1 abstraction-occurence association
this.course = course;
private Course course;
RegistrationList = new LinkedList();
}
// The 1-many association to class Registration
private List registationList;
public void cancel()
{
// The following are present only to determine
//to ‘Cancelled’ state
//the state
open = false;
// The initial state is ‘Planned’
closedOrCancelled = true;
private boolean open = false;
unregisterStudents();
private boolean closedOrCancelled = false;
}
...
}

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 43 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 44
Example: The CourseSection class Example: The CourseSection class
public void openRegistration() public void closeRegistration()
{ {
if(!closedOrCancelled) //to 'Cancelled' or 'Closed' state
// must be in ‘Planned’ state open = false;
{ closedOrCancelled = true;
open = true; if(registrationList.size() <
//to 'OpenNotEnoughStudents' state course.getMinimu m())
} {
} unregisterStudents();
//to ‘Cancelled’ state
}
}

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 45 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 46

Example: The CourseSection class Example: The CourseSection class


public void requestToRegister(Student student) // Activity associated with ‘Cancelled’ state.
{ private void unregisterStudents()
if(open) // must be in one of the two 'Open' states {
{ Iteratorit = registrationList.iterator();
// The interaction specified in the sequence diagram while (it.hasNext())
Course prereq = course.getPrerequisite(); {
if(student.hasPassedCourse(prereq)) Registration r = (Registration)it.next();
{ r.unregisterStudent();
//Indirectly calls addToRegistrationList it.remove();
new Registration(this, student); }
} }

// Check for auto matic transition to 'Closed' state // Called within this package only, by the
if(registrationList.size() >= course.getMaxim u m()) // constructor of Registration
{ void addToRegistrationList(
//to ‘Closed’ state Registration newRegistration)
open = false; {
closedOrCancelled = true; registrationList.add(newRegistration);
} }
} }
}

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 47 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 48
8.5 Difficulties and Risks in Modelling
Interactions and Behaviour
Dynamic modelling is a difficult skill
In a large system there are a very large number of possible paths a
system can take.
It is hard to choose the classes to which to allocate each behaviour:
—Ensure that skilled developers lead the process, and ensure that
all aspects of your models are properly reviewed.
—Work iteratively:
- Develop initial class diagrams, use cases, responsibilities,
interaction diagrams and state diagrams;
- Then go back and verify that all of these are consistent, modifying
them as necessary.
—Drawing different diagrams that capture related, but distinct,
information will often highlight problems.

© Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour 49

You might also like