50.003 - Sequence Diagrams¶
Learning Outcomes¶
By the end of this unit, you should be able to
- Define a UML sequence diagram
- Describe elements of UML sequence diagram
- Identify the elements of a given UML sequence diagram
Sequence Diagram - Introduction¶
A sequence diagram is an interaction diagram that details how operations are carried out. It shows how objects communicate with other objects to invoke methods/constructors in a sequential manner. These communications are shown as messages. A sequence diagram typically represents a single use case or a specific scenario.
By using a Sequence Diagram you can;
-
Visualize message flow. Sequence diagrams clarify how different system parts interact and what messages they exchange.
-
Document system behavior. Sequence diagrams document a system’s behavior, making it easier to understand how it works and identify potential issues.
-
Identify errors. Sequence diagrams can help identify potential errors in the system by highlighting miscommunications or unexpected interactions between objects.
-
Improve communication. Sequence diagrams provide a common language for developers, designers, and other stakeholders to discuss system interactions.
A sequence diagram consists of two axes namely, object axis and time axis which interpretes the participants and relative order given a message. Object axis shows different objects involved in the task under consideration (can be derived from class diagram). Time axis shows the relative ordering. i.e. messages represented lower in the time axis happen later (See below)
.
Elements of a Sequence Diagram¶
The following are the main elements of a sequence diagram.
-
Actor - Actor is an external entity interacting with the system. For a given use case, the sequence diagram can reuse the same set of actors.
-
Object - An object is an internal entity involved in an interaction. The interaction can be between participants or between a participant and an actor.
-
Lifeline - A life line is a vertical dashed line starting from a participant (object or actor) indicating the existence of the participant over time.
-
Messages - Messages indicate communication between two participants (e.g., method calls, constructors, returning results) by an arrow and a message label. Arrow starts from the lifeline of the message sender and ends at the lifeline of the message recipient.
-
Activation Bar - Time during which an object is active and denoted by a vertical thin rectangle . This typically indicate the duration taken by the object to perform the task specified. Activation bar appears at the point of receiving the message. End of the activation bar indicates end of the execution of the task initiated by the message.
Notation for Sequence Diagram Elements¶
See below for visual representations of sequence diagram elements introduced above.
Actor and Lifeline | Participants and Lifelines |
Activation after Message |
Notation for Sequence Diagram Messages¶
![]() |
![]() |
---|---|
Synchronous Message | Asynchronous Message |
Synchronous message and Asynchronous message are represented solid arrows. For synchronous message, the arrow head is dark while for the asynchronous message the arrow head is not dark. When this arrow meets the receiving life line, an activation bar can be activated optionally.
They represent synchronous and asynchronous methods invokations respectively.
![]() |
---|
Return Message |
Return message is shown by a dotted arrow with a dark or non dark arrow head (depending on the asynchronous nature) from life line 2 to life line 1. Return message always has a corresponding synchronous or asynchronous message sent from life line 1 to life line 2 (opposite direction). If this corresponding message activated an activation bar at life line 2, it should be deactivated at the beginning of return message arrow.
A return message refers to returning a value from an invoked method.
![]() |
![]() |
---|---|
Create Message | Destroy Message |
A create message is a special type of a synchrounous message which represents an invokation of object constructor. The arrow starts from life line 1, and ends at a newly created life line 2.
![]() |
---|
Self Message |
A self message is a message from a life line to itself denoted by a looping arrow. Optionally we can show a nested activation bar from the start of the arrow to the end of the arrow.
A self message represents an object/class method calling another method (or the same method) in the same class.
![]() |
![]() |
---|---|
Found Message | Lost Message |
In found message and lost message, one party involved in the communication is external to the system. The found messge is shown by an arrow starting from outside the system boundary and ending at a life line while a lost message is shown by an arrow starting from a lifeline and ending outside the system boundary.
![]() |
---|
Duration Message |
Duration message is shown as a downslanted arrow in contrast to straight horizontal arrow in other messages.
This means the network/processing delay of the method represented is significant and needs to be highlighted.
Notation for Composed Sequence Diagram Elements¶
![]() |
---|
Conditional composition |
Conditional composition is shown by a rectangle with the top left header 'alt' (meaning alternatives). The rectangle is split to multiple horizontal sub rectangles by dotted lines. Each sub rectangle represents a sequence of messages communicated under a given condition, specified on top within square brackets.
![]() |
---|
Loop composition |
Loop composition is shown by a rectangle with the top left header 'loop'. The iterating parameter is specified within square brackets on top.
![]() |
---|
Parallel composition |
Parallel composition is shown by a rectangle with the top left header 'par' (meaning parallel). The rectangle is split to multiple horizontal sub rectangles by dotted lines. The sequence of messages contained in each sub rectangle are said to be communicated in parallel (independantly).
Possible implementation of Sequence Diagram Elements¶
messages¶
- Synchronous message
A synchronous message is a method invokation of the recipient object which waits for the return message before advancing to the next operation. In other words, current line and next operation execute one after the other (Synchronous).
In the below code example, the method add(a,b) called in calc object and the next operation is assigning it to result. Here add(a,b) and the assigment operation happens one after the other (i.e. Synchronously).
Asynchronous message is a method invokation of recipient object (api) which does not wait for the return message before advancing to the next operation. In other words, current line and next operation can concurrently execute (Asynchronous).
Here, fetch('/api/dataB') is called before resolving the promise for fetch('/api/dataA') meaning both fetch methods running in the background concurrently.
Return message represents returning back to the calling object with or without a result.
In the code example below, calling object invokes 'fetchData()' on 'fet' object. 'fet' object sends the return message 'Data fetched'.
A create message is a message sent for starting a lifeline (new object).
In the below code example, the 'new User('Alice')' object is created.
Destroy message is sent to destroy an object so that it is not available for use afterwords.
In the code example below, User object is created first via a create message. Later, Controller class's terminate method is used to destroy that User object.
Self message is a message sent by an object to itself. In code, any internal (this.something()) from the object's class itself is a self message.
In the example, the logger1 object's 'log' method execution can be shown by an activation bar. inside 'log' method 'this.format(message)' can be denoted as a self message because the method call is within the same object. A recursive message is a special type of self message where a new activation bar is included in the existing one.
Found messages are those that arrive from an unknown sender, or from a sender not shown on the current diagram due to coming from an external party. The message is said to be found by the receiving object which is in the system.
In the code example, 'handleClick' method can be triggered by a user click which is invoked by a user which is not a part of the system. However, 'handleClick' method is run by an object in the system.
Lost messages are those that are either sent but do not arrive at the intended recipient, or which go to a recipient not shown on the current diagram.
In the code example, sendAnalytics method is run in a third party service even though it is called from the system. Hence, in the system perspective the message is lost.
If it takes a significant time to perform the task in the message, we typically represent is as a duration message (a slanted arrow).
In the code example, we show the method 'processHeavyTask()' called by 'server' object. In the sequence diagram, it is a slanted arrow from server Client object to the 'server' object with the message 'processHeavyTask()'.
Composed Elements¶
- Conditional (Alternatives) Alternative composition is a combined fragment representing a mutually exclusive choice between different sequences of messages.
In the code example, there are two choices namely, successful login and failed login. During successful login, 'displayDashboard()' is invoked, while during failed login, 'displayError()' is invoked which are two alternative message sequences in the sequence diagram.
- Loop Loop composition is a combined fragment representing a message sequence which is repeatedly executed (with the condition for ending repetition).
In the code example, the invokation 'selectItem(item)' is executed for each item in the cart. Which are repetitive message sequences in sequence diagram.
In the code example, 'taskA()' and 'taskB()' are asynchronous functions returning promises and work on their resolved promises independantly. When both are done, the 'combinedResult' is calculated since the 'await' blocks the 'combinedResult' until the