Sequence Diagram
UML & System Modelling
While a Class Diagram shows the static structure of a system, it doesn't show how objects interact over time. Consider an online purchase: a User
clicks "Checkout", a PaymentGateway
is called, an Order
is created, and an EmailService
sends a confirmation. How do you visualize this sequence of events?
This is where Sequence Diagrams is used. They are a type of UML interaction diagram that shows how processes operate with one another and in what order. They are essential for modeling the dynamic behavior of a system.
A Sequence Diagram illustrates object interactions arranged in a time sequence. It depicts the objects involved in the scenario and the sequence of messages exchanged between the objects needed to carry out the functionality of the scenario.
Core Components of a Sequence Diagram
A sequence diagram is read from top to bottom.
- Participants (Lifelines): Represented by a box with a dashed line extending downwards. Each participant is an object or entity involved in the interaction.
- Messages: Arrows between lifelines that represent communication.
- Synchronous Message: A solid arrow (
->
). The sender waits for the receiver to finish processing the message before continuing. This is like a standard function call. - Asynchronous Message: An open-headed arrow (
-->
). The sender does not wait for the receiver; it continues its own processing immediately. This is like firing an event or a message queue. - Reply Message: A dashed arrow (
<--
). Indicates the return from a synchronous message.
- Synchronous Message: A solid arrow (
- Activation Bars (Execution Occurrences): Thin rectangles on a lifeline that show the period during which an object is performing an action.
Modeling a User Login Flow
Let's model a common scenario: a user attempting to log in to a web application.
Scenario:
- The User enters their credentials into the
LoginScreen
. - The
LoginScreen
sends the credentials to theAuthController
. - The
AuthController
hashes the password and asks theUserService
to find the user. - The
UserService
queries theDatabase
for the user record. - The
Database
returns the user data (or null). - The
UserService
returns theUser
object to theAuthController
. - The
AuthController
compares the hashed password with the one from the database. - If they match, it creates a
Session
and returns a success message to theLoginScreen
.
This diagram is invaluable for a few reasons:
- Clarity: It makes the flow of control explicit and easy to follow.
- Identifying Bottlenecks: You can visually identify potential performance issues. For example, seeing multiple, sequential calls to a database might prompt a discussion about optimization.
- API Design: It helps define the public methods needed for each class. The arrows pointing to a lifeline directly translate to methods that class must expose (e.g.,
AuthController
needs alogin()
method,UserService
needs afindUserByEmail()
method).
Sequence diagrams are thus useful for moving from static class structures to dynamic, real-world interactions. They help in understanding how different parts of a system work together over time, making them a key part of the LLD toolkit.