Professional Documents
Culture Documents
Lecture 7
Sequence Diagrams
(UML@Classroom – chapter 6 )
1
Contents
2
Process map from Sun Microsystems
3
UML diagrams
• Class diagrams
– model the static structure of the system
• Interaction diagrams
– model the dynamic aspects of the system
4
5
UML diagrams
• Sequence diagrams
• Communication diagrams
– (collaboration diagrams)
6
Sequence & Communication diagrams
BOTH of these diagrams show Inter-Object behaviour –
that is, interaction BETWEEN Objects within the system
• Sequence diagrams :
– show interactions and emphasise the order of the messages
over time
8
Interactions
Interaction diagrams can also zoom in much further into the system to be
realized and can model communication at class level, meaning that you
can use them to model operation calls and inter-object behaviour.
9
Sequence Diagrams
• Model the high level interaction between active objects in a
system
• They are a graphical representation of a Use Case
• Display the interactions that take place between the objects in a
Use Case
• Show the sequence of messages between objects in a use case
Includes :
– Frames
– Participants/Objects
– Lifelines
– Messages and Focus of Control
– Actors
11
Overview
• Sequence Diagrams have a Vertical Time Axis
Frame
Time
Lifelines
12
Frame
– Contains the Sequence Diagram
– Indicates the context of the Sequence Diagram
– Not always used
Heading
sd interaction name
Content area
13
Participants (Interaction Partners)
The Participants are placed in rectangles along the top of the Sequence
Diagram
Syntax Explanation
o An object named o
o:C An object named o of class c
:C An anonymous object of class c
o[i] The object o that is selected by the index
value i
s ref sd3 A subsystem s whose internal interaction is
shown in sequence diagram sd3
15
Lifelines
• Denotes the life of a participant (Object) during a
sequence
Participant name
Lifeline notation
16
Execution Occurrence
• A long narrow rectangle placed on top of a lifeline
• Denotes when an Object (participant) is sending or
receiving messages
Participant name
Execution Occurrence
17
Message & Reply notation
• A Message conveys information from one Object to Another
• An Operation is labelled with the message being sent and it is a solid arrow
• Whereas the Return is labelled with the value being returned and shown as
a dashed line
message
19
Message & Reply notation
Message Syntax:
[attribute=] signal-or-operation-name [(arguments)] [:return-value]
message
anOrder anOrderLine
getQuantity
quantity = getQuantity
22
User Interaction
In many cases, it is this User Interaction which :
• identifies the operations
• ‘controls’ the order of these operations
-> Derived from the Use Case Form/Activity Diagram
26
Class Diagram
28
Sample Screen
• Will be based on :
– Main Flow Of Events
• Will match the order of events
– objects/attributes from the class diagram
29
Sample Screen
30
Sample Screen
- with operations
- matches the flow of events
1. CourseID – from user
- Use [ square brackets ]
2. DisplayCourseDetails()
- From Course Class
3. DisplayDepartmentDetails(
- From Department Class
4. DisplayModuleDetails()
- From Module Class
- Done in a loop
31
Class Diagram
– with flow of events
1. CourseID – from Admin
Access the Course Class
2. DisplayCourseDetails
Within the Course Class
3. DisplayDepartmentDetails 4. DisplayModuleDetails
FROM the Course Class FROM the Course Class
In a LOOP
33
Sequence Diagram
Additional Information about the previous Sequence Diagram :
34
Example
For another example, we will now look at a
another use case scenario
35
For an example, let us look at a simple use
case scenario
Use case “Calculate price”
We have an order and are going to calculate its price.
To do this the order (object) needs to :
• look at all the order lines in the order
• multiply the number of items in an order line by the product’s unit price,
• and then add up the prices of all the order lines.
Finally the order (object) has to check with the customer (object)
to see if he/she gets a discount, and use this to calculate the end
total.
Main Flow of
Events 1. Get the quantity, product & product price
2. Calculate order line price total
3. Find out if customer gets discount
4. Calculate final order price
Alternative flow
Of Events
Post-condition
Non-Functional
Requirements
Issues
37
Relevant Information
We will probably have to hold the following
data :
• Order Information
– (Order Number, DateReceived )
• Customer Information
– ( name, address, discount etc)
• Order Line(s)
– Product, Quantity
– ( e.g. Camera, 2)
• Product
– Price (e.g. Camera Price = £100)
38
• Produce an initial Class Diagram
– Identify classes
– Identify attributes for each class
– Identify possible operations
39
Order Customer
1 .. *
Initial Class Diagram – with attributes
Order Line
only
-quantity : Integer
-product : String
What operations do we need ??
41
Order Customer
Order Line In order to carry out the task – calculate the order price
– the objects have to communicate with each other .
-quantity : Integer They communicate using their operations. This is what
-product : String
we mean when we talk of passing messages.
+getQuantity()
+getProduct() Look at each class to see what information objects of the
* class would hold and what operations they can carry out
(use CRC Cards)
1
Product
These operations can also be added/updated
-price : Money
when producing the Sequence Diagrams
+getPrice() 42
Order Example
will be used to explain how to produce the
Sequence Diagram
The following Customer has placed an order for a camera:
Customer First Name : Joe
Customer Last Name : Smith
Address : 15 Any St
Discount : £10
Order no : 5555 Date : 15/6/16
Product Price Qty
Camera 100 2
Order Line
Product
-price : 100
44
Main Flow of Events :
45
Order Customer
3b
-datereceived : 15/6/16 -lastName: Smith
-number : 5555 -firstName : Joe
-discount: 10
2 -calcBasePrice()
-address : 9 Any St.
-calcDiscount()
3a +calcPrice()
+getDiscount()
1a 1b 1c
4 Need to calculate price of a specific Order …. 5555 :
calcPrice() .. Will do the following for Order 5555:
1a Get the quantity (from Order-Line) 2 -> getQuantity()
Order Line 1b Get product (from Order-Line) Camera -> getProduct()
1c & then get product-price (from Product) 100 -> getPrice()
-quantity : 2
-product : Camera
2 Calculate Order Line Price total (called the ‘Base price’)
+getQuantity()
= (quantity * product-price) = 2*100 =200
+getProduct()
-> calcBasePrice()
3a Calculate Discount : -> calcDiscount()
3b get customer discount (from Customer) 10
Product
-> getDiscount()
-price : 100
+getPrice()
4 Calculate final order price ….. using Discount
46
-> done by calcPrice … Order price = 200-10 =190
Refer to the Module Hand-out for a full description
calcPrice
getQuantity and getProduct are both ‘obvious’ operations - it is clear what they do
In this diagram – there is no return from ‘getQuantity – but there is a return from getProduct
(i.e. aProduct() )
Both of these are correct – do not overuse ‘return’ values that are ‘obvious’ 47
lifeline
return
activation
(method)
self-call
message
48
Order Customer
1 .. *
Order Line
-quantity : Integer
-product : String
+getQuantity()
+getProduct()
*
1
Product
-price : Money
+getPrice()
Order Customer
-datereceived : Date
* 1 -lastName: String
-number : String -firstName : String
-discount: Double
-addLinePriceToTotal(price:double) : double -address : Address
+calcPrice()
+getDiscountedTotal(total:double) : double
1
For Information ONLY :
An Alternative Class diagram – we are not going to use
these classes with their slightly different operations, so you
1 .. * can just ignore this slide. However, it demonstrates an
Order Line important point.
-quantity : Integer This class diagram has the same four classes, but the
-product : String operations (methods) are different. This is a more
decentralised approach to the problem of calculating the
+calcPrice(): double Order price.
51
Message Type Notation
• Synchronous or Call
• Asynchronous
• Creation
• Destruction
52
Synchronous Messages
53
As we have already discussed :
If the message is a Synchronous message, when one method invokes another method
(sends a message), the focus of control is passed to the other operation (method). Control
returns to the first operation with the return arrow, or at end of execution rectangle.
The message is shown with an arrow with filled in arrowhead, and the return has an arrow
with a dashed line. The arrowhead on the return can be drawn filled in or open (the UML
specifications are a bit ambiguous)
55
Note that some applications are multi-threaded, which means that it’s possible
for a method to send a message and still keep focus of control (i.e. it does not
have to wait for the invoked method to finish and return control).
In these cases both methods are running at the same time. This type of
message is called an Asynchronous message and is shown using an open
arrowhead.
DeleteObject
A message to destroy
a participant (usually
an object) 58
Lost & Found Messages
Lost message
Found messages
message
Combined Fragments :
(UML@Classroom – chapter 6.4 )
61
Common operators for interaction frames
- we will look at some of these in the following slides
loop Loop; the fragment may execute multiple times, and the
guard indicates the basis of the iteration
region Critical region; the fragment can only have one thread
executing at once
message
Loop[condition]
message
63
A loop for each
Order Line Item
[else] message
calcPrice()
getQuantity()
getProduct()
aProduct
getPrice()
calcBasePrice()
calcDiscPrice()
getDiscount()
opt [total price > aaa
aDrawTicket
createDrawTicket()
Ref : Interaction Occurrences
• This is a way to hide details in sequence diagrams
ref
Write File to Print (“UML.doc”)
ref
Send File to Printer (“UML.doc”)
68
sd Write File to Print (String name)
:WordProcessor :PrintSpooler
open(name)
f:File
write(document)
close
print(name)
69
User Interaction
• Clerk and OrderScreen (or ‘System’) are NOT classes – they have been
added to show the User Interaction, ( which comes from the Use Case
Form / Activity Diagram) :
– information input by User ( e.g Clerk)
– Information displayed on Screen (OrderScreen)
May also May also
use ‘Actor’ be called
Symbol ‘System’