# 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.

## Types of message

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

## 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

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) {
total = total + in.getAmount();
}

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

public double getTotal() {
}
}

class SavingAmount {
private double amount;

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

public double getAmount() {
return amount;
}
}``````