Sequence Diagram in UML
In the last article, we saw Activity diagrams, the notations to be used in Activity diagrams, their significance, and how to build an Activity diagram. We then made an Activity diagram for a specific scenario in our Courseware Management system. One of the most widely used dynamic diagrams in UML is the Sequence diagram, which is the topic of our discussion today. By the end of this article, you will know what a Sequence diagram is, what its elements are, and, you will be able to create Sequence diagrams for your system.
A Sequence diagram depicts the sequence of actions that occur in a system. The invocation of methods in each object, and the order in which the invocation occurs is captured in a Sequence diagram. This makes the Sequence diagram a very useful tool to easily represent the dynamic behavior of a system.
A Sequence diagram is two-dimensional in nature. On the horizontal axis, it shows the life of the object that it represents, while on the vertical axis, it shows the sequence of the creation or invocation of these objects.
Because it uses class name and object name references, the Sequence diagram is very useful in elaborating and detailing the dynamic design and the sequence and origin of invocation of objects. Hence, the Sequence diagram is one of the most widely used dynamic diagrams in UML.
Defining a Sequence diagram
A sequence diagram is made up of objects and messages. Objects are represented exactly how they have been represented in all UML diagrams—as rectangles with the underlined class name within the rectangle. A skeleton sequence diagram is shown in Figure 8.1. We shall discuss each of these elements in the next section:
Figure 8.1: Sample Sequence diagram, showing the general elements of a sequence diagram
An object can call itself recursively. An arrow commencing and ending at itself denotes this.
Defining a Sequence diagram
Let us take a look at the building blocks of a Sequence diagram.
Elements of a Sequence diagram
A Sequence diagram consists of the following behavioral elements:
|Element and its description||Symbol|
|Object: The primary element involved in a sequence diagram is an Object—an instance of a class. A Sequence diagram consists of sequences of interaction among different objects over a period of time. An object is represented by a named rectangle. The name to the left of the ":" is the object name and to its right is the class name.|
|Message: The interaction between different objects in a sequence diagram is represented as messages. A message is denoted by a directed arrow. Depending on the type of message, the notation differs. In a Sequence diagram, you can represent simple messages, special messages to create or destroy objects, and message responses.|
Creating a Sequence Diagram
If your UML tool supports modeling class diagrams, you should be able to model Sequence diagrams as well. We will be using the Poseidon Community Edition tool to draw the class diagram. You can use any tool that you are comfortable with.
Figure 8.2: a screen shot of the Poseidon tool.
The screen shot of the Poseidon tool in Figure 8.2 shows the different options to model Sequence diagrams and define interactions between objects participating in these interactions.
Case Study—Courseware Management System
From the discussion in the previous section, we are clear on the different notations that are used in Sequence diagrams. Armed with this knowledge, we will proceed to design a Sequence diagram for our Courseware Management System case study application. Because a Sequence diagram represents the dynamic flows in an application, we will aim to represent one of the flows using a Sequence diagram. In Article 3, we had defined the following use cases for the Courseware Management System:
- View courses
- Manage topics for a course
- Manage course information
- View course calendar
- View tutors
- Manage tutor information
- Assign courses to tutors
For these use cases, we had modeled the classes and interfaces using the class diagram in Article 4 (parts 1 and 2). Now, we will combine the flow defined by the use cases and the classes involved in the use cases together to represent the different flows in the Courseware Management System.
As an example, we will represent the "Manage course information" flow using a Sequence diagram.
Identifying the activities and transitions for managing course information
The "Manage course information" flow contains one participant: the Course Administrator. Apart from this, there are a few entities with which the course administrator interacts in this flow—Course, Topic, and Tutor. The sequence of steps carried out in the "Manage course information" flow are:
- A user who is a course administrator invokes the manage course functionality.
- The manage course functionality of the course administrator invokes either the course creation or course modification functionality of a course.
- After the course is either created or modified, the manage topic functionality of the course administrator calls the topic creation or modification functionality of a topic.
- Finally, the user invokes the assign tutor to course functionality of the course administrator to assign a tutor to the selected course.
Now, let us model these steps into a Sequence diagram for the "Manage course information" functionality.
Figure 8.3: Activity diagram for the "Manage course information" flow
Figure 8.3 shows the Sequence diagram for the "Manage course information" flow. The Sequence diagram uses the CourseAdministrator, Course, Topic, and Tutor classes that we had defined earlier (see Article 4, parts 1 and 2) when we modeled the different classes in the Courseware Management System. The methods used in the Sequence diagram are the same methods that we had defined for these classes previously.
A Sequence diagram, as we saw today, represents the interaction among the different objects of a system. In the next article of this series, we will learn about Collaboration diagrams that are similar to Sequence diagrams, but represented as a set of graphs.
About the Authors
Mandar S. Chitnis, Lakshmi Ananthamurthy, and Pravin S. Tiwari are the co-founders of Novusware, Inc. They have co-authored the book Teach Yourself BEA WebLogic Server 7.0 in 21 Days (Sams Publishing, Oct. 2002), based on the recently launched WebLogic Server 7.0 by BEA Systems, Inc.
For any questions or queries regarding the article's contents, please contact firstname.lastname@example.org.