CPSC 210

C5: Extracting Sequence Diagrams

Learning Goals

  • Extract a sequence diagram from a given Java code base

Sequence Diagrams

  • Depicts calls between objects while system is running
  • Starts with a particular method call
A a = new A();
B b = new B();
a.doA(b);

public class A {
    public void doA(B b) {
        b.doB();
    }
}

public class B {
    public void doB() { }
}
Objects
Method Calls
Execution Timeline
Lifelines

Calls to the same object

A a = new A();
B b = new B();
a.doA(b);

public class A {
    public void doA(B b) {
        b.doB();
    }

public class B {
    public void doB() {
        this.doB2();
    }

    public void doB2() { }
}
Call to same object

Parts of the Diagram (again)

Method's execution timeline

Lifelines

Method Calls

Objects alive throughout the execution

Loops & Conditionals

public class RegistrationSystem {
  private List<Student> students;

  public void printRecords() {
    for (Student s : students) {
      Rec rec = s.getRec();
      // ...
    }
  }
}

Loop box

Same for conditionals!

if: ...

else if: ...

else: ...

Real
Example

Note: exact notation may differ between variations

Lecture Ticket Review

public class Street {
  public static void main(String[] args) {
    Magician magician = new Magician();
    Onlooker carl = new Onlooker();
    magician.attractAudience(carl);
    magician.doMagicTrick();
  }
}
public class Magician {
  private Onlooker audienceMember = null;
  public void doMagicTrick() {
    audienceMember.beAmazed();
  }
  public void attractAudience(Onlooker a) {
    audienceMember = a;
  }
}
public class Onlooker {
  public void beAmazed() {
    gasp();
  }
  private void gasp() {
  }
}
Magician
magician
Onlooker
carl
beAmazed()
doMagicTrick()
gasp()

Lecture Lab

Recommended procedure:

  • Sketch your objects and lifelines
    • How many lifelines do we need?
  • Sketch your loop/alternation boxes
    • How many loop/alternation boxes do we need?
@Override
public void printLog(EventLog el) {
  for (Event next : el) {
    logArea.setText(logArea.getText() + next.toString() + "\n\n");
  }

  repaint();
}

Lecture Lab Hint

@Override
public void printLog(EventLog el) {
  for (Event next : el) {
    logArea.setText(logArea.getText() + next.toString() + "\n\n");
  }

  repaint();
}

Text

C5: Extracting Sequence Diagrams

The End - Thank You!

Made with Slides.com