A sequence diagram models the interactions among objects. Typically a sequence diagram captures the interactions of a single scenario – i.e., the “sequence of steps describing an interaction between a user and a system” (Fowler 2004).

Consider the following source code (adapted from “Guessing Game,” Chris Healy, CS 11, Furman University) and corresponding sequence diagram:

Scanner kbd = new Scanner(System.in);
Guess game = new Guess(); // initialize the game

boolean gameOver = false;
while (! gameOver) { // loop until we win or lose the game
  System.out.printf("Enter your guess: ");
  int newGuess = kbd.nextInt();
  if (game.good(newGuess)) {
    System.out.printf("Congratulations! You guessed my number!\n");
    gameOver = true;
  } else if (game.youLose()) {
    System.out.printf("Sorry, you have run out of guesses.\n");
    System.out.printf("The answer was %d.\n", game.giveAnswer());
    gameOver = true;
  }
}

sequence diagram

Create sequence diagrams for the following examples (solutions):

  1. Turtle graphics is a vector graphics environment with packages available for a variety of programming languages. Model the code shown below:

    def draw_square(turtle, x, y, size):
        '''Draw a square of the specified size at the location specified'''
    
        turtle.setposition(x, y)
    
        turtle.forward(size)
        turtle.right(90)
    
        turtle.forward(size)
        turtle.right(90)
    
        turtle.forward(size)
        turtle.right(90)
    
        turtle.forward(size)
        turtle.right(90)
    
    
    draw_square(turtle, 100, 100, 300)
    turtle.done()
    

    turtle graphics sequence diagram

  2. The Model-View-Controller (MVC) architecture has three components that interact with each other. Model the interactions when the controller’s service method is invoked.

    public class Model<T> {
      private T data;
    
      public T getData() {
        return this.data;
      }
    }
    
    public class View {
      public <T> View(Model<T> model) {
        T data = model.getData();
        // finish initialization
      }
    }
    
    public class Controller {
      private Model model;
    
      public void service(Request request, Response response) {
        View view = new View(this.model);
        response.setView(view);
      }
    }
    

    model-view-controller (MVC) sequence diagram

  3. In Sudoku, a player attempts to fill a 9x9 grid with digits such that each column, row, and 3x3 subgrid contain all the digits from 1 to 9. Model the invocation of a setEntry(row, column, value) method that calls isValid() to determine if the value is valid for the location and returns the result.

    Sudoku sequence diagram

  4. The shuffle method (that accepts no arguments) of a deck of cards:

    public class Deck {
      private Card[] cards;
    
      public void shuffle() {
        Random random = new Random();
        shuffle(random);
      }
    
      public void shuffle(Random random) {
        for (int i = 0; i < this.cards.length; i++) {
          int position = i + random.nextInt(this.cards.length - i);
          swap(i, position);
        }
      }
    
      private void swap(int i, int j) {
        Card card = this.cards[i];
        this.cards[i] = this.cards[j];
        this.cards[j] = card;
      }
    }
    

    shuffle sequence diagram

  5. In the Observer design pattern, when an object changes its state, it notifies all its dependents (i.e., observers) of the change. Model the invocation of the setState method.

    public abstract class Observer {
      private Subject subject;
    
      public void update() {
        Object o = this.subject.getState();
        // do something with the state
      }
    }
    
    public class Subject {
      private Object state;
      private List<Observer> observers;
    
      public void getState() {
        return this.state;
      }
    
      public void setState(Object o) {
        this.state = o;
        notify();
      }
    
      public void notify() {
        for (Observer observer : observers) {
          observer.update();
        }
      }
    }
    

    observer sequence diagram