AMSS Lecture 7: UML State Diagrams

Traian-Florin Șerbănuță

2025

Agenda

Session 1: Foundations of State Diagrams

Session 2: Advanced Modeling

Session 3: Code Mapping

Session 1: Foundations of State Diagrams

What Are State Diagrams?

Definition

A UML State Machine Diagram models the lifecycle of an object — the states it goes through and how it transitions between them.

Use cases

Key Elements

Components of Transitions

Syntax

trigger [guard] / action
Trigger

event that initiates the transition (e.g., coin, push)

Guard

condition that must be true for transition to occur (e.g., [balance > 0])

Activity / Action

operation executed when the transition occurs (e.g., unlock(), displayMessage())

Notes on transitions

Syntax

trigger [guard] / action

Example: Gothic Castle Safe

Example: ATM Session Lifecycle

Interactive Exercise: Identify Missing Transitions

Scenario: ATM State Diagram

Task: Add error or timeout transitions to the diagram.

Recap

Session 2: Advanced Modeling

Superstates (Composite States) and Substates

Problem

If several states share transitions / internal activities

Solution

Group them in a superstate and move shared behavior to it

Example: ATM State Diagram with Composite States

Internal Activities

Activity States (do-activities)

Concurrent (Orthogonal) States

Some systems have independent aspects of behavior.

Example: Smartphone with concurrent regions:

History Pseudo-States

Used to remember the last active substate when re-entering a composite state.

Example: Alarm clock with radio

Session 3: Code Mapping

Remember the Turnstile State Diagram

Using the State Design Pattern

From UML to Code (Java Example)

Simple State Pattern implementation mapping UML concepts to code:

Source file

class Turnstile {
    private State state = new Locked(this);
    void setState(State s) { state = s; }
    void coin() { state.handleCoin(); }
    void push() { state.handlePush(); }
}

class State {
    Turnstile turnstile;
    State(Turnstile turnstile) {
        this.turnstile = turnstile;
    }
    public void handleCoin() {
        printState();
        System.out.println("Trigger: Coin inserted!");
    }
    public void handlePush() {
        printState();
        System.out.println("Trigger: Push attempted!");
    }
    void printState() {
        System.out.println("State: ".concat(this.getClass().getName()));
    }
}

class Locked extends State {
    Locked(Turnstile turnstile) {
        super(turnstile);
    }
    public void handleCoin() {
        super.handleCoin();
        unlock();
        turnstile.setState(new Unlocked(this.turnstile));
    }
    public void handlePush() {
        super.handlePush();
        System.out.println("Action: You shall not pass!");   
    }
    void unlock() {
        System.out.println("Action: Turnstile unlocked");
    }
}

class Unlocked extends State {
    Unlocked(Turnstile turnstile) {
        super(turnstile);
    }
    public void handleCoin() {
        super.handleCoin();
        System.out.println("Action: Turnstile already unlocked. returning coin.");   
    }
    public void handlePush() {
        super.handlePush();
        System.out.println("\tPerson passed.");   
        lock();
        turnstile.setState(new Locked(this.turnstile));
    }
    void lock() {
        System.out.println("Action: Turnstile locked");
    }
}

public class TurnstileDemo {
    public static void main(String[] args) {
        Turnstile t = new Turnstile();
        t.coin();
        t.push();
        t.coin();
        t.push();
        t.coin();
        t.coin();
        t.push();
        t.push();
    }
}

Interactive Task

Description

A smart home system has two major features –Security System and Climate Control– operating concurrently when powered on.

The system starts in an Off state and transitions to Operational when powered on.

Requirements

Wrap-Up

Concept Description Example
Simple state Mode of behavior Locked/Unlocked
Composite state Grouped states Playing (Buffering, Streaming)
Internal activities transitions within same state entry/exit
Activity state States performing work do/ search
Concurrent state Independent regions Connectivity, AppState
History Remembers previous substate Resume after pause

Takeaway: UML state diagrams help capture dynamic, event-driven aspects of systems and bridge toward implementable designs.