What is a State Diagram?
A State Diagram (also called a State Machine Diagram) models the different states an object can be in and the transitions between those states. It shows how an object responds to events throughout its lifecycle β from creation to destruction.
State diagrams are perfect for objects that have distinct behavioral modes. An order can be "pending," "confirmed," "shipped," or "delivered." A TCP connection can be "listening," "syn-sent," "established," or "closed." Each state has different valid operations and behaviors.
State Diagram Notation
Basic Elements:
1. Initial State (solid circle)
β
2. Final State (circle with X)
β
3. State (rounded rectangle)
ββββββββββββββββββββββ
β State Name β
ββββββββββββββββββββββ€
β entry / action β β entry action
β do / activity β β ongoing activity
β exit / action β β exit action
ββββββββββββββββββββββ
4. Transition (solid arrow)
βββββββΆ
Label format: event [guard] / action
5. Decision Node (diamond)
β
Example: Order Lifecycle
Order State Diagram:
β
β
βΌ
βββββββββββββββββββ
β Pending β entry / initializeOrder()
β β do / validateItems()
ββββββββββ¬βββββββββ
β
β [all items in stock] / reserveInventory()
βΌ
βββββββββββββββββββ
β Confirmed β entry / sendConfirmation()
β β do / processPayment()
ββββββββββ¬βββββββββ
β
ββββββ΄βββββ
β β
βΌ βΌ
ββββββββββ ββββββββββββ
β Shippedβ β Cancelledβ
β β β β
βββββ¬βββββ ββββββββββββ
β β±
β [delivered] β±
β / updateStatus β±
βΌ β±
ββββββββββββββββ β±
β Delivered β β±
β β β±
ββββββββββββββββ
β
States:
β’ Pending β order created, waiting for confirmation
β’ Confirmed β payment processed, waiting for shipment
β’ Shipped β in transit to customer
β’ Delivered β received by customer
β’ Cancelled β order was cancelled
Guard Conditions and Actions
Transitions can include conditions (guards) that must be true for the transition to occur, and actions that execute when the transition happens:
Transition Label Format:
event [guard condition] / action
Examples:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
paymentReceived [amount >= total] / confirmOrder()
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β β
β β β ββ action to execute
β β ββ condition that must be true
β ββ the event that triggers the transition
ββ name of the event
More Examples:
click [itemInCart] / checkout()
timeout [retries < 3] / retry()
signal [signalStrength > 80] / establishConnection()
do / processQueue() β internal activity, not event-triggered
Composite States
When states themselves have internal states, you can use composite states (hierarchical state machines):
Connection State Diagram with Composite States:
β
β
βΌ
ββββββββββββββββββββββββββββββββββββββ
β Disconnected β
ββββββββββββββββββ¬ββββββββββββββββββββ
β connect()
βΌ
ββββββββββββββββββββββββββββββββββββββ
β Connecting β
β ββββββββββββββββββββββββββββββββ β
β β TCP Handshake β β
β ββββββββββββββββββββββββββββββββ β
ββββββββββββββββββ¬ββββββββββββββββββββ
β connected
βΌ
ββββββββββββββββββββββββββββββββββββββ
β Connected β
β βββββββββββ ββββββββββββββββ β
β β Idle βββββΆβ Transferringβ β
β βββββββββββ ββββββββββββββββ β
β β² β β
β βββββββββββββββββ β
ββββββββββββββββββ¬ββββββββββββββββββββ
β disconnect()
βΌ
ββββββββββββββββββββββββββββββββββββββ
β Disconnected β
ββββββββββββββββββββββββββββββββββββββ
β
β
Inside Connected, the object can be Idle or Transferring.
Events to Connected are routed to the current sub-state.
Entry, Do, and Exit Actions
States can have three types of internal actions:
ββββββββββββββββββββββββββββββββ
β Processing β
ββββββββββββββββββββββββββββββββ€
β entry / startTimer() β β runs when entering the state
β do / processItems() β β runs while in the state
β exit / stopTimer() β β runs when leaving the state
ββββββββββββββββββββββββββββββββ
entry β Executes once when the state is entered.
Good for setup: start timers, log messages, acquire resources.
do β Executes continuously (or repeatedly) while in the state.
Good for ongoing work: processing, monitoring, waiting.
exit β Executes once when leaving the state.
Good for cleanup: stop timers, release resources, save data.
Real Example β Download State:
βββββββββββββββββββββββββββββββββββ
β Downloading β
βββββββββββββββββββββββββββββββββββ€
β entry / startDownload() β β connects to server
β do / receiveData() β β receives chunks
β exit / saveFile() β β writes to disk
βββββββββββββββββββββββββββββββββββ
Events:
start β (from Idle)
complete β (to Completed)
error β (to Error)
cancel β (to Cancelled)