Sequence Diagrams

ICT2622 - Object-Oriented Analysis · Object-Oriented Modelling Techniques

Sequence Diagrams

Sequence diagrams are a type of interaction diagram in the Unified Modeling Language (UML). They show how objects interact in a particular scenario of a use case. Sequence diagrams illustrate the order of messages exchanged between objects and the timing of these messages.

Components of Sequence Diagrams

Sequence diagrams consist of several key components:

  • Actors: External entities that interact with the system, represented by stick figures.
  • Objects: Instances of classes that participate in the interaction, represented by rectangles.
  • Lifelines: Vertical dashed lines that represent the presence of an object over time.
  • Messages: Horizontal arrows that indicate the communication between objects. They can represent synchronous messages (calls) or asynchronous messages (signals).
  • Activation boxes: Rectangles on lifelines that show when an object is active or controlling the flow of messages.

Creating a Sequence Diagram

To create a sequence diagram, follow these steps:

  1. Identify the scenario: Choose a use case or a specific scenario to model.
  2. Identify the actors and objects: Determine which actors and objects are involved in the scenario.
  3. Define the messages: List the messages exchanged between the actors and objects in the order they occur.
  4. Draw the diagram: Start by drawing the actors and objects, then add lifelines, activation boxes, and messages.

Example of a Sequence Diagram

Let us consider a simple scenario where a customer places an order for a product in an online store. The main actors and objects involved are:

  • Actor: Customer
  • Object: Order System
  • Object: Payment Gateway

The steps in the scenario are as follows:

  1. The customer selects a product.
  2. The customer places an order.
  3. The order system processes the order.
  4. The order system requests payment from the payment gateway.
  5. The payment gateway processes the payment.
  6. The payment gateway sends a confirmation to the order system.
  7. The order system confirms the order to the customer.

Now, we can create the sequence diagram for this scenario. The diagram will look like this:

<Customer> --> <Order System>: Select Product
<Customer> --> <Order System>: Place Order
<Order System> --> <Payment Gateway>: Request Payment
<Payment Gateway> --> <Order System>: Confirm Payment
<Order System> --> <Customer>: Confirm Order

Types of Messages

In sequence diagrams, messages can be classified into several types:

  • Synchronous messages: Indicated by a solid line arrow, these messages require a response before the sender can continue. For example, when the order system requests payment, it waits for a confirmation before proceeding.
  • Asynchronous messages: Indicated by a dashed line arrow, these messages do not require an immediate response. The sender continues with its process without waiting. For example, when the system sends a notification to the customer.
  • Return messages: These are responses sent back to the caller. They are often shown as dashed arrows.

Watch out for common mistakes

Watch out: Students often forget to indicate the type of message (synchronous or asynchronous) in their diagrams. This can lead to misunderstandings about the flow of control.

Best Practices for Sequence Diagrams

When creating sequence diagrams, consider the following best practices:

  • Keep the diagram simple and focused on the specific scenario.
  • Use clear and descriptive names for actors and objects.
  • Limit the number of messages to avoid clutter.
  • Ensure that the sequence of messages is logical and reflects the actual process.

Advanced Concepts

As you become more familiar with sequence diagrams, you may encounter some advanced concepts:

  • Combined fragments: These are used to represent alternative flows or loops in the sequence. They are enclosed in a box with a label indicating the type of fragment, such as alt for alternatives or loop for repeated actions.
  • Interaction overview diagrams: These diagrams provide a higher-level view of the interactions between objects and can include multiple sequence diagrams.

Example of a Combined Fragment

Suppose we want to model a scenario where the customer can either pay with a credit card or choose cash on delivery. We can represent this using a combined fragment:

alt Pay with Credit Card
  <Customer> --> <Payment Gateway>: Enter Credit Card Details
  <Payment Gateway> --> <Customer>: Payment Confirmation
else Pay with Cash
  <Customer> --> <Order System>: Choose Cash on Delivery
end

Summary

  • Sequence diagrams show the order of messages exchanged between objects in a scenario.
  • Key components include actors, objects, lifelines, messages, and activation boxes.
  • Messages can be synchronous or asynchronous, and return messages are also included.
  • Best practices include keeping diagrams simple and using descriptive names.
  • Advanced concepts include combined fragments for alternative flows.

Check your understanding

  1. What are the key components of a sequence diagram?
  2. How do you identify synchronous and asynchronous messages in a sequence diagram?
  3. What is the purpose of a combined fragment in a sequence diagram?
  4. Why is it important to keep sequence diagrams simple?