LLDUML & System ModellingSequence Diagram

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.

  1. Participants (Lifelines): Represented by a box with a dashed line extending downwards. Each participant is an object or entity involved in the interaction.
  2. 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.
  3. Activation Bars (Execution Occurrences): Thin rectangles on a lifeline that show the period during which an object is performing an action.
ClientServer1: Synchronous Message2: Reply Message

Modeling a User Login Flow

Let's model a common scenario: a user attempting to log in to a web application.

Scenario:

  1. The User enters their credentials into the LoginScreen.
  2. The LoginScreen sends the credentials to the AuthController.
  3. The AuthController hashes the password and asks the UserService to find the user.
  4. The UserService queries the Database for the user record.
  5. The Database returns the user data (or null).
  6. The UserService returns the User object to the AuthController.
  7. The AuthController compares the hashed password with the one from the database.
  8. If they match, it creates a Session and returns a success message to the LoginScreen.

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 a login() method, UserService needs a findUserByEmail() 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.