State diagrams: composite states and history
简介 / Introduction
State machines describe the lifecycle of objects and protocol state transitions. Composite states bundle multiple sub-states under one high-level state, reducing diagram clutter; history states remember the last active sub-state on exit, enabling “resume where you left off”. This article uses the same business scenario (a login flow) to demonstrate PlantUML and Mermaid syntax side by side.
PlantUML 版
PlantUML supports full composite states plus shallow/deep history markers:
1 | @startuml |
PlantUML key points:
state <Name> { ... }defines a composite state; sub-states and branches live inside.[*]inside the composite block marks the default entry sub-state.H <<History>>is shallow history (one level);H* <<DeepHistory>>resumes across arbitrary depth.- Stereotypes
<<History>>/<<DeepHistory>>need matchingskinparam state BackgroundColorto render with distinct colors.
Mermaid 版
Mermaid does not support composite states or history states. Mermaid’s stateDiagram only supports flat orthogonal regions (separated by --), no nested state blocks, and no H / H* history markers. Below is the equivalent flat representation — it cannot express “resume from last interrupted sub-state”:
1 | stateDiagram-v2 |
If your domain requires composite/history semantics, use PlantUML. Mermaid (v11) currently has no plans to support them.
对比 / Side-by-side
| Feature | PlantUML | Mermaid |
|---|---|---|
| Composite state (nested sub-states) | ✅ state X { ... } |
❌ Not supported |
Shallow history H |
✅ <<History>> |
❌ Not supported |
Deep history H* |
✅ <<DeepHistory>> |
❌ Not supported |
| Default entry sub-state | ✅ [*] inside composite |
N/A |
| Branch orthogonal region | ✅ -- separator |
✅ -- separator |
| Per-state color skinparam | ✅ Full support | ⚠️ Global fill only |
小结 / Wrap-up
PlantUML’s state machine notation fully supports composite states and history states, making it suitable for complex protocols and workflow engines. Mermaid stateDiagram targets lightweight use cases; if your design depends on “interrupt-and-resume” semantics, stick with PlantUML. Choose based on state complexity — Mermaid for quick flat flows, PlantUML when depth and history matter.
- Title: State diagrams: composite states and history
- Author: puml.online
- Created at : 2026-09-05 09:00:00
- Updated at : 2026-09-05 01:06:44
- Link: https://puml.online/blog/state-diagram-composite-en/
- License: This work is licensed under CC BY-NC-SA 4.0.