Complete guide to UML State Diagrams (State Machine Diagrams): learn how to model object lifecycles, state transitions, and state-dependent behavior. Master state machines for designing robust systems.
Table of Contents
1. What are State Diagrams?
State diagrams (also called state machine diagrams or statecharts) model the lifecycle of objects that have distinct states. They show the states an object can be in, the transitions between states, and the events that trigger those transitions.
State diagrams are particularly useful for:
- Modeling objects with complex state-dependent behavior
- Documenting object lifecycles
- Designing stateful systems
- Understanding when certain operations are valid
- Identifying all possible states and transitions
Use state diagrams when an object's behavior depends on its current state and when the object transitions between a finite number of states in response to events.
2. States
2.1 What is a State?
A state represents a condition or situation during the life of an object in which it satisfies some condition, performs some activity, or waits for some event. States are shown as rounded rectangles.
2.2 State Notation
2.3 State Components
- State Name: Identifies the state
- Entry Action: Action executed when entering the state
- Do Activity: Ongoing activity while in the state
- Exit Action: Action executed when leaving the state
2.4 State Types
- Simple State: Basic state with no substates
- Composite State: State containing substates
- Submachine State: State that references another state machine
- History State: Remembers the last active substate
3. Transitions
3.1 What is a Transition?
A transition represents a change from one state to another in response to an event. Transitions are shown as arrows connecting states.
3.2 Transition Notation
3.3 Transition Components
- Event: What triggers the transition (e.g., "orderPlaced", "paymentReceived")
- Guard Condition: Boolean condition that must be true for transition to occur (e.g., [amount > 0])
- Action: Behavior executed during the transition (e.g., /sendConfirmation())
3.4 Transition Syntax
Full transition syntax:
event[guard]/action
Examples:
paymentReceived[amount > 0]/sendConfirmation()
timeout/notifyUser()
cancel/cancelOrder()
3.5 Self-Transitions
A self-transition returns to the same state, triggering entry and exit actions:
4. Initial and Final States
4.1 Initial State
The initial state (pseudostate) marks the starting point of a state machine. Shown as a filled circle.
4.2 Final State
The final state marks the end of a state machine. Shown as a filled circle with a surrounding circle.
4.3 Using Initial and Final States
- Every state machine should have at least one initial state
- Final states are optional (object may not terminate)
- Composite states can have their own initial/final states
- Multiple final states can represent different termination conditions
5. Composite States
5.1 What are Composite States?
Composite states contain substates, allowing you to model hierarchical state machines. They help manage complexity by grouping related states.
5.2 Composite State Notation
5.3 History States
A history state (shallow or deep) remembers the last active substate when re-entering a composite state:
5.4 Concurrent States
States can be divided into concurrent regions (orthogonal regions) that execute simultaneously:
6. Best Practices
6.1 Identify All States
- List all possible states the object can be in
- Consider error states and exceptional conditions
- Include intermediate states, not just start and end
- Validate states with domain experts
6.2 Define Clear Transitions
- Every transition should have a clear trigger (event)
- Use guard conditions to clarify when transitions occur
- Ensure all states are reachable
- Avoid unreachable states
6.3 Use Composite States
- Group related states into composite states
- Use composite states to manage complexity
- Keep state machines readable and understandable
- Don't nest too deeply (2-3 levels usually sufficient)
6.4 Document Actions
- Document entry/exit actions clearly
- Specify transition actions
- Keep actions simple and focused
- Use consistent naming conventions
6.5 Validate Completeness
- Ensure all valid transitions are modeled
- Check for missing states or transitions
- Verify state machine handles all scenarios
- Test state machine logic
0 Comments