UML Sequence Diagram

Sequence diagrams were covered as part of the Software Engineering module (COM00144M) during my MSc studies with the University of York. This post will detail my findings and provide information and examples on how to create sequence diagrams.

What is a sequence diagram?

A sequence diagram is a type of interaction diagram that shows interactions via time sequence. Each object has a vertically descending lifeline with operation arrows from the source to target lifeline.

Sequence diagram components

Here are examples of some of the components that can be used in a sequence diagram.

ActorA user who interacts with or is external to the system.
Start PointCan be used to show the start point.
Object SymbolAn object symbol represents a class or object, the naming should be as demonstrated in the image.
LifelineA lifeline is a dashed vertical line and represents the passing of time.
ActivationAn activation is the duration of an event, the larger the box the longer the event takes to complete.
MessageA message is communication between lifelines in the form of object interaction through the sending and receiving of messages.
LoopA loop box can be used to signify the condition of which a loop will run and highlight the sub-section.

Types of message

Here are some examples of the different types of messages that can be sent in sequence diagrams.

Synchronous MessageSynchronous messages wait for a return message, this is shown via a dashed line.
Asynchronous MessageAsynchronous messages do not wait for a return message.
Create MessageA create message is used to show the instantiation of a new object.
Self MessageCan be used to show calling a method located within the current object.
Recursive MessageCan be used to show to show recursion within the current object.

Guards

There can be times when a message should only be sent if a particular condition is met. In sequence diagrams we can use guards to achieve this.

To add a guard you simple need to precondition your message with a constraint wrapped in square brackets [balance > 10].

Example sequence diagram guard

Example sequence diagram

Now that we know more about what components we can use in a sequence diagram, let’s take an example scenario and create a sequence diagram for that.

public class Bank {
    public static void main(String[] args) {
        Bank bk = new Bank();
        bk.open();
    }

    public void open() {
        SavingAccount sa = new SavingAccount();

        SavingAmount saving1 = new SavingAmount(25.10);
        SavingAmount withdraw1 = new SavingAmount(15);
        sa.deposit(saving1);
        sa.withdraw(withdraw1);

        System.out.println(sa.getTotal());
    }
}

class SavingAccount {
    Set<SavingAmount> s = null;
    private double total;

    SavingAccount() {
        s = new HashSet<SavingAmount>();
    }

    public void deposit(SavingAmount in) {
        s.add(in);
        total = total + in.getAmount();
    }

    public void withdraw(SavingAmount out) {
        s.remove(out);
        total = total - out.getAmount();
    }

    public double getTotal() {
        return total;
    }
}

class SavingAmount {
    private double amount;

    public SavingAmount(double amountIn) {
        amount = amountIn;
    }

    public double getAmount() {
        return amount;
    }
}
Example sequence diagram of the java snippet

Leave a Reply

Your email address will not be published.