Sequence Diagram
Sequence Diagram
Donald Bell is an IT architect for IBM Rational software, where he works with clients to define
and adopt effective software delivery practices and tools.
Summary: From The Rational Edge series of articles, .UML basics, on the essential diagrams in
the Unified Modeling Language, this article offers a detailed introduction to the sequence
diagram. It also introduces several new notation elements in the recent UML 2.0 specification.
View more content in this series
Tags for this article: diagram, sequence, sequqnce, training, tutorial, uml, uml2
Tag this!
Update My dW interests (Log in | What's this?) Skip to help for Update My dW interests
Date: 16 Feb 2004
Level: Introductory
Also available in: Chinese Vietnamese
Back to top
The notation
Since this is the first article in my UML diagram series that is based on UML 2, we need to first
discuss an addition to the notation in UML 2 diagrams, namely a notation element called a
frame. The frame element is used as a basis for many other diagram elements in UML 2, but the
first place most people will encounter a frame element is as the graphical boundary of a diagram.
A frame element provides a consistent place for a diagram's label, while providing a graphical
boundary for the diagram. The frame element is optional in UML diagrams; as you can see in
Figures 1 and 2, the diagram's label is placed in the top left corner in what I'll call the frame's
"namebox," a sort of dog-eared rectangle, and the actual UML diagram is defined within the
body of the larger enclosing rectangle.
Figure 1: An empty UML 2 frame element
In addition to providing a visual border, the frame element also has an important functional use
in diagrams depicting interactions, such as the sequence diagram. On sequence diagrams
incoming and outgoing messages (a.k.a. interactions) for a sequence can be modeled by
connecting the messages to the border of the frame element (as seen in Figure 2). This will be
covered in more detail in the "Beyond the basics" section below.
Notice that in Figure 2 the diagram's label begins with the letters "sd," for Sequence Diagram.
When using a frame element to enclose a diagram, the diagram's label needs to follow the format
of:
Diagram Type Diagram Name
The UML specification provides specific text values for diagram types (e.g., sd = Sequence
Diagram, activity = Activity Diagram, and use case = Use Case Diagram).
Back to top
The basics
The main purpose of a sequence diagram is to define event sequences that result in some desired
outcome. The focus is less on messages themselves and more on the order in which messages
occur; nevertheless, most sequence diagrams will communicate what messages are sent between
a system's objects as well as the order in which they occur. The diagram conveys this
information along the horizontal and vertical dimensions: the vertical dimension shows, top
down, the time sequence of messages/calls as they occur, and the horizontal dimension shows,
left to right, the object instances that the messages are sent to.
Lifelines
When drawing a sequence diagram, lifeline notation elements are placed across the top of the
diagram. Lifelines represent either roles or object instances that participate in the sequence being
modeled. [Note: In fully modeled systems the objects (instances of classes) will also be modeled
on a system's class diagram.] Lifelines are drawn as a box with a dashed line descending from
the center of the bottom edge (Figure 3). The lifeline's name is placed inside the box.
Figure 3: An example of the Student class used in a lifeline whose instance name is
freshman
The UML standard for naming a lifeline follows the format of:
Instance Name : Class Name
In the example shown in Figure 3, the lifeline represents an instance of the class Student, whose
instance name is freshman. Note that, here, the lifeline name is underlined. When an underline is
used, it means that the lifeline represents a specific instance of a class in a sequence diagram, and
not a particular kind of instance (i.e., a role). In a future article we'll look at structure modeling.
For now, just observe that sequence diagrams may include roles (such as buyer and seller)
without specifying who plays those roles (such as Bill and Fred). This allows diagram reuse in
different contexts. Simply put, instance names in sequence diagrams are underlined; roles names
are not.
Our example lifeline in Figure 3 is a named object, but not all lifelines represent named objects.
Instead a lifeline can be used to represent an anonymous or unnamed instance. When modeling
an unnamed instance on a sequence diagram, the lifeline's name follows the same pattern as a
named instance; but instead of providing an instance name, that portion of the lifeline's name is
left blank. Again referring to Figure 3, if the lifeline is representing an anonymous instance of
the Student class, the lifeline would be: " Student." Also, because sequence diagrams are used
during the design phase of projects, it is completely legitimate to have an object whose type is
unspecified: for example, "freshman."
Messages
The first message of a sequence diagram always starts at the top and is typically located on the
left side of the diagram for readability. Subsequent messages are then added to the diagram
slightly lower then the previous message.
To show an object (i.e., lifeline) sending a message to another object, you draw a line to the
receiving object with a solid arrowhead (if a synchronous call operation) or with a stick
arrowhead (if an asynchronous signal). The message/method name is placed above the arrowed
line. The message that is being sent to the receiving object represents an operation/method that
the receiving object's class implements. In the example in Figure 4, the analyst object makes a
call to the system object which is an instance of the ReportingSystem class. The analyst object is
calling the system object's getAvailableReports method. The system object then calls the
getSecurityClearance method with the argument of userId on the secSystem object, which is of
the class type SecuritySystem. [Note: When reading this sequence diagram, assume that the
analyst has already logged into the system.]
Besides just showing message calls on the sequence diagram, the Figure 4 diagram includes
return messages. These return messages are optional; a return message is drawn as a dotted line
with an open arrowhead back to the originating lifeline, and above this dotted line you place the
return value from the operation. In Figure 4 the secSystem object returns userClearance to the
system object when the getSecurityClearance method is called. The system object returns
availableReports when the getAvailableReports method is called.
Again, the return messages are an optional part of a sequence diagram. The use of return
messages depends on the level of detail/abstraction that is being modeled. Return messages are
useful if finer detail is required; otherwise, the invocation message is sufficient. I personally like
to include return messages whenever a value will be returned, because I find the extra details
make a sequence diagram easier to read.
When modeling a sequence diagram, there will be times that an object will need to send a
message to itself. When does an object call itself? A purist would argue that an object should
never send a message to itself. However, modeling an object sending a message to itself can be
useful in some cases. For example, Figure 5 is an improved version of Figure 4. The Figure 5
version shows the system object calling its determineAvailableReports method. By showing the
system sending itself the message "determineAvailableReports," the model draws attention to the
fact that this processing takes place in the system object.
To draw an object calling itself, you draw a message as you would normally, but instead of
connecting it to another object, you connect the message back to the object itself.
The example messages in Figure 5 show synchronous messages; however, in sequence diagrams
you can model asynchronous messages, too. An asynchronous message is drawn similar to a
synchronous one, but the message's line is drawn with a stick arrowhead, as shown in Figure 6.
Guards
When modeling object interactions, there will be times when a condition must be met for a
message to be sent to the object. Guards are used throughout UML diagrams to control flow.
Here, I will discuss guards in both UML 1.x as well as UML 2.0. In UML 1.x, a guard could
only be assigned to a single message. To draw a guard on a sequence diagram in UML 1.x, you
placed the guard element above the message line being guarded and in front of the message
name. Figure 7 shows a fragment of a sequence diagram with a guard on the message addStudent
method.
Figure 7: A segment of a UML 1.x sequence diagram in which the addStudent message has
a guard
In Figure 7, the guard is the text "[pastDueBalance = 0]." By having the guard on this message,
the addStudent message will only be sent if the accounts receivable system returns a past due
balance of zero. The notation of a guard is very simple; the format is:
[Boolean Test]
For example,
[pastDueBalance = 0]
As an example to show how an alternative combination fragment is read, Figure 8 shows the
sequence starting at the top, with the bank object getting the check's amount and the account's
balance. At this point in the sequence the alternative combination fragment takes over. Because
of the guard "[balance >= amount]," if the account's balance is greater than or equal to the
amount, then the sequence continues with the bank object sending the addDebitTransaction and
storePhotoOfCheck messages to the account object. However, if the balance is not greater than
or equal to the amount, then the sequence proceeds with the bank object sending the
addInsuffientFundFee and noteReturnedCheck message to the account object and the
returnCheck message to itself. The second sequence is called when the balance is not greater
than or equal to the amount because of the "[else]" guard. In alternative combination fragments,
the "[else]" guard is not required; and if an operand does not have an explicit guard on it, then
the "[else]" guard is to be assumed.
Alternative combination fragments are not limited to simple "if then else" tests. There can be as
many alternative paths as are needed. If more alternatives are needed, all you must do is add an
operand to the rectangle with that sequence's guard and messages.
Option
The option combination fragment is used to model a sequence that, given a certain condition,
will occur; otherwise, the sequence does not occur. An option is used to model a simple "if then"
statement (i.e., if there are fewer than five donuts on the shelf, then make two dozen more
donuts).
The option combination fragment notation is similar to the alternation combination fragment,
except that it only has one operand and there never can be an "else" guard (it just does not make
sense here). To draw an option combination you draw a frame. The text "opt" is placed inside the
frame's namebox, and in the frame's content area the option's guard is placed towards the top left
corner on top of a lifeline. Then the option's sequence of messages is placed in the remainder of
the frame's content area. These elements are illustrated in Figure 9.
Figure 9: A sequence diagram fragment that includes an option combination fragment