Traian-Florin Șerbănuță
2025
A UML State Machine Diagram models the lifecycle of an object — the states it goes through and how it transitions between them.
trigger [guard] / action
event that initiates the transition (e.g., coin,
push)
condition that must be true for transition to occur (e.g.,
[balance > 0])
operation executed when the transition occurs (e.g.,
unlock(), displayMessage())
trigger [guard] / action
Scenario: ATM State Diagram
Task: Add error or timeout transitions to the diagram.
If several states share transitions / internal activities
Group them in a superstate and move shared behavior to it
React to events without changing state
Similar to a self-transition
trigger[guard]/action within the
stateSpecial entry/exit activities
States in which the object is doing some ongoing work
Once ongoing activity is completed, a transition with no event is taken
If an event occurs before the ongoing activity completes, activity is stopped
Some systems have independent aspects of behavior.
Example: Smartphone with concurrent regions:
Used to remember the last active substate when re-entering a composite state.
In the diagram the history state points to the default state.
Deep history (H*) can be used to remember nested
hierarchy of substates
Simple State Pattern implementation mapping UML concepts to code:
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();
}
}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.
When entering Operational, the system initializes.
While Running, two subsystems work in parallel:
SecuritySystem
ClimateControl
Heats when temperature is below min threshold
Cools when temperature is above max threshold
Returns to Idle once temperature normalizes.
The user can power Off the system at anytime
| 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.