UML Interaction Diagrams
Michael L. Collard, Ph.D. Department of Computer Science
Kent State University
I Gede Made Karma
Types of Diagrams
• Structural Diagrams – focus on static
aspects of the software system
–
Class
, Object, Component, Deployment
Kent State University 2
j
p
p y
• Behavioral Diagrams – focus on dynamic
aspects of the software system
–
Use-case
,
Interaction
, State Chart, Activity
Behavioral Diagrams
•
Use Case Diagram
– high-level behaviors of the
system, user goals, external entities: actors
•
Sequence Diagram
– focus on time ordering of
messages
Kent State University 3
g
•
Collaboration Diagram
– focus on structural
organization of objects and messages
•
State Chart Diagram
– event driven state
changes of system
•
Activity Diagram
– flow of control between
activities
Refining the Object Model
• Typically, only very simplistic object models can be directly derived from use cases.
• A better understanding of the behavior of each use case is necessary (i.e., analysis)
Kent State University 4
• Use interaction diagrams to specify and detail the
behavior of use cases
• This helps to identify and refine key abstractions and relationships
• Operations, attributes, and messages are also identified during this process
Interaction Diagrams
• There is one (or more) Interaction diagram per use case
– Represent a sequence of interactions – Made up of objects, links, and messages
• Sequence diagrams
Kent State University 5
q g
– Models flow of control by time ordering – Emphasizes passing messages wrt time – Shows simple iteration and branching
• Collaboration diagrams
– Models flow of control by organization
– Structural relationships among instances in the interaction – Shows complex iteration and branching
Collaboration Diagrams
For each system operation
identified in the Use Case Diagrams, and
described in a contract
Design a system of interacting objects to perform the operation.
The collaboration diagram will indicate the objects participating in the collaboration, and
Collaboration Diagrams
Actor System
System Op
+
:ClassA SystemOp
Operation:xxx
Use Case Diagram
+
:ClassB
1: message1()
Collaboration Diagram Contract
p
Precondition:..
Alternatively (or perhaps primarily)
Sequence Diagrams can be used
instead of Collaboration Diagrams
Sequence Diagrams
Actor System
System Op
+
Operation:xxx:ClassA :ClassB
SystemOp
message1()
Use Case Diagram
+
Contract
Precondition:..
Collaboration Diagram
Collaboration Diagrams
Notation
Student :Student John:Student
class instance named instance
The Collaboration diagram should consist of illustrations of the messages sent to objects of the classes identified in the creates/destroys, reads, changes sections of the contract. The system operation will be directed to one of the objects in this group that will act as the controller for executing this task.
Collaboration Diagrams
Illustrating links
UML standard syntax for messages
return := message(parameter : parameterType) : returnType return : message(parameter : parameterType) : returnType
optional
1: tot := total():integer
:POST :Sale
sysOp()
sender receiver
Collaboration Diagrams
Illustrating Iteration
1*: [i:=0..9] str : getName(): String
SystemOp( )
[ ] g () g
CS335:ClassList :Grader
Sequence number followed by
a * indicates iteration Iteration clause is optional
Collaboration Diagrams
Illustrating Iteration
In the example described on the previous slide, we assumed that the target object contained a sequence of names (simple strings) as an attribute. More generally
the ClassList may be considered a collection of y
StudentRec objects, and messages may be directed to the collection. The depiction of a message to a multiobject – a group of objects stored in a collection – is indicated on the next slide.
:ClassA 1: message( )
Representation of a collection (multiobject)
Collaboration Diagrams
Given: A vector of StudentRecord
v: vector 1: inx := find(name: String): integer
V i 1 t ll ti (b t t i NOT
Version 1 – message to a collection (but vector is NOT a concept in the problem domain)
:StudentRecord 1: inx := find(name: String): integer
Version 2 – (preferred) Emphasizes the objects in the collection, not (the solution domain) container.
Collaboration Diagrams
Conditional Paths – mutually exclusive messages
:ClassA :ClassB
msg1( )
1a: [test] msg2( )
guard – if true send msg
:ClassC :ClassD
1b: [not test] msg4( )
1b.1: msg5( )
1a.1: msg3( )
Collaboration Diagrams
Creating a new instance during implementation of the system operation
New
1 t ( )
c: Controller m:MenuItem
:MenuItem 1: create( )
2: add(m)
Create a new MenuItem, m, and add it to the Menu
Collaboration Diagrams
• Emphasizes the organization of the objects
that participate in an interaction
• Classifier roles
Kent State University 17
C ss e
o es
• Association
• Messages, flow, and sequencing
Example Collaboration Diagram
Request(order, customer) 2: cost:=researce(order)
Kent State University 18
orderTaker TicketDB
CreditBureau
1: checkC redit(cus
tomer) 3: debit(cust
Sequence Diagrams
Sequence diagrams offer an alternate way of expressing the exchange of messages that occur between objects in an object-based system.
The notation for objects and collections is the same as used in collaboration diagrams. Time ordering is from top to bottom.
:ClassA :ClassB
msg1( )
Focus of control is illustrated with an activation box
msg2( ) msg3( )
Each message between objects is represented with a message expression on an arrowed line between the objects
Sequence Diagrams
Additional Notation
:ClassA :ClassB
Object creation -- let the classB object create an instance of a classC object.
Newly created class is placed in a column starting at the level
msg1( )
msg2( )
msg3( ) :ClassC
create( ) msg4( )
starting at the level (time) of creation.
time
Sequence Diagrams
:ClassA :ClassB :ClassC Conditional message – has guard to specify the conditionMutually exclusive conditional message
[flag = true] msg2( ) msg1( )
[flag = false] msg3( )
Sequence Diagrams
:ClassA :ClassB
Iteration for a single message
:ClassC Iteration for a sequence of messages
msg1( )
*[i = 1..N] msg2( ) msg3( ) msg4( )
msg5( )
*[k = 1..M]
Enclose the repeated sequence in a box, and indicate the multiplicity
Sequence Diagrams
• X-axis is objects
– Object that initiates interaction is left most – Object to the right are increasingly more subordinate
Y
i i ti
Kent State University 23
• Y-axis is time
– Messages sent and received are ordered by time
• Object life lines represent the existence over a
period of time
• Activation (double line) is the execution of the
procedure.
Example: Sequence Diagram
c:client p:planningAssistant
:TicketAgent <<create>>
Kent State University 24
setItinerary(i)
calculateRoute()
route
<<destroy>>
Message Passing
• Send – sends a signal (message) to an object
• Return – returns a value to a caller
• Call
invoke an operation
Kent State University 25
• Call – invoke an operation
• Stereotypes
– <<create>>
– <<destroy>>
call send
Return
Example
S : sampler WD : sensors WS : sensors Temp : sensors Hum : sensors
Every 1/60 sec.
Kent State University 26
Every 0.5 sec.
Every 5 min.
Properties of Sequence Diagrams
• Initiator is leftmost object (boundary object)
• Next is typically a control object
• Then comes entity objects
Kent State University 27
• Then comes entity objects
Collaboration vs Sequence
• The two diagrams really show the same
information
• Collaboration diagrams show more static
Kent State University 28
g
structure (however, class diagrams are
better at this)
• Sequence diagrams clearly highlight the
orderings and very useful for multi-tasking
Refining the Class Diagram
For every class, examine each collaboration diagram and identify
1. Every message received by an instance of that class – (these will be the methods for that class)
2 Each object (class) to which an instance of this class
2. Each object (class) to which an instance of this class sends a message – (these may require reference attributes within the class, depending upon the visibility)
Refining the Class Diagram
Class name
Data attributes
Classes to which
methods
Class Description
Link attributes
State Transition Diagrams
State transition diagrams are a useful tool for constructing the individual classes. Specifically, they aid in two important ways in “fleshing out” the structure of the class:
1. method development -- State transition diagrams provide the “blueprints” for developing the algorithms
p p p g g
that implement methods in the class
2. attribute identification – Attributes contain the state information needed for regulating the behaviors of the instances of the class
When constructing state transition diagrams, take care to ensure that the post-conditions stipulated in the contracts are enforced.
State Transition Diagrams
Notation
St t 2 event|output
Transitions are labeled with the triggering event and the output if any Some events do not
trigger a change in state
start
State 1 State 2
final state
States are represented with an oval and label
State transitions are represented with a directed arc or line
State Transition Diagrams
Additional Notation
State State
event|output
[boolean condition]
[ ]
Guard condition – transition occurs only if condition is true
State Transition Diagrams
Example – Nested States in Telephone Call
active idle
off hook|dial tone
[valid subscriber]
talking
on hook playing dial do: hang up
tone
dialing connecting
digit
digit
complete|ringing answered The state labeled
active has substates
State Transition Diagrams
Second Example – A Queue of Capacity Two
Size0 Size1 size2
arrival arrival
arrival | balk departure
departure
The queue has three states that indicate its number of occupants. When the queue is full, new arrivals cannot enter, and must leave the system.
Description of Class Queue
classQueue {
private:
intcapacity, size;
public:
Queue(intcap);
voidenqueue(Object obj);
State
State transitions
//precondition: queue not full //post condition: if (queue not full) // size = size + 1 // else no change Object dequeue();
Visibility
For an object A to send a message to object B, B must be visible to A.
In determining the visibility of one object to another, we must
d id d i th d i h th th li k b t bj t A
decide during the design whether the link between objects A and B is:
permanent or transient
exclusive or shared
fixed or variable
Visibility
Example 1. permanent, exclusive , and fixed
Consider a clock composed of three Counters (hours, mins, secs)
Clock Counter
modulus
*
moduluscount 3
•The Counters have the same lifetime as the Clock – They
are created with the clock and dissolved when the clock is out of scope.
•The Counters are bound exclusively to the Clock – They
cannot be referenced by any other object in the system
Visibility
Implementation of the association between Clock and Counters
classClock {
private:
Counter hours mins secs; Counter hours, mins, secs;
public:
Counter( ) : hours(24), mins(60), secs(60) {}; //other methods
}
C++ header file
Visibility
Example 2. permanent, shared, fixed
ProductSpec Computer
*
describesThe ProductSpec exists before an instance of the product is created. That product (computer) will have a permanent association with its ProductSpec that it will share access to with other computers of the same model.
Visibility
(Partial) Specification of class Computer
classComputer {
private:
ProductSpec & ps;
bli public:
Computer (ProductSpec & theMod) : ps(theMod) {}; //other methods
}
The reference variable ps must be initialized in an initialization list, and cannot be reassigned during the life of the object.
Visibility
Attribute visibility -- an association that must be remembered is implemented as attribute visibility. We have just seen a couple of examples of attribute associativity. In C++ attribute associativity is provided via three forms of the declaration
Counter c;
Stack * s;
ProductSpec & ps;
permanent and exclusive; lifetime of the counter is bounded by the lifetime of the object itself.
Can be non-permanent, variable, and shared
Visibility
Transient visibility is not remembered, but persists only during the execution of a single method. It is implemented in one of two ways.
Parameter visibility – object C is passed from A to B
:ProductCatalog
:POST :Sale
2: spec:= specification(upc) 3: makeLineItem(spec, qty)
1: [new sale] create()
A ProductSpec object is passed to the Sale object for use by its makeLineItem method
Visibility
Transient associations may also be implemented with locally declared visibility. Consider a method breadfirstSearch that uses an auxiliary Queue to perform a breadth first traversal of a binary tree. The lifetime of the Queue object will be within the scope of the method, and the reference need not exist outside of this method.
Review
In designing and constructing an object-oriented system one must:
1. From a statement of the requirements and from the use cases identify the objects (concepts) that occur in the “problem domain”
2. Construct a Concept Diagram showing the static relationship (long-p g g p ( g term associations) between concepts
3. Identify the concepts that are outside of the system to be built (Actors) and use Sequence Diagrams to identify the events (system operations) that they generate
4. For each system operation construct a contract that stipulates the state of the system before and after the operation (pre- and post- conditions) and identifies the concepts that collaborate in performing the operation 5. Elaboration – Review your models for lack of clarity and inconsistency
and add or modify as necessary.
Review (continued)
6. Enhance the Concept model by exploring the associations in greater detail (roles, aggregation, generalization, link attributes, etc) and iteratively add design concepts from the solution domain (programming concepts)
7. Construct Sequence Diagrams or Collaboration diagrams (for each system operation) to detail the sequence of messages that must be exchanged to implement the taskg p
8. For each relevant object (or collection of aggregates) use a State Chart Diagram (state transition diagram) to detail the states of the system and the sequence of transitions that can occur during its operation
9. Continue elaboration of the models until the system is well understood and class specifications (In C++, header files) can be constructed.