**状态机工作流**
patterns
此工作流模拟随时间跟踪对象状态变化。
完整 FlowZap 代码
OrderSystem { # Order System
n1: circle label:"Start"
n2: rectangle label:"Receive order event"
n3: rectangle label:"Load current state"
n4: rectangle label:"Order completed"
n5: circle label:"End"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> n3.handle(left)
n3.handle(bottom) -> StateMachine.n6.handle(top) [label="Process"]
n4.handle(right) -> n5.handle(left)
}
StateMachine { # State Machine
n6: diamond label:"Current state?"
n7: rectangle label:"Pending -> Confirmed"
n8: rectangle label:"Confirmed -> Processing"
n9: rectangle label:"Processing -> Shipped"
n10: rectangle label:"Shipped -> Delivered"
n11: rectangle label:"Any -> Cancelled"
n12: diamond label:"Valid transition?"
n13: rectangle label:"Update state"
n14: rectangle label:"Reject invalid transition"
n6.handle(right) -> n7.handle(left) [label="Pending"]
n6.handle(bottom) -> n8.handle(top) [label="Confirmed"]
n6.handle(left) -> n9.handle(top) [label="Processing"]
n7.handle(bottom) -> n12.handle(top)
n8.handle(bottom) -> n12.handle(top)
n9.handle(bottom) -> n12.handle(top)
n10.handle(bottom) -> n12.handle(top)
n11.handle(bottom) -> n12.handle(top)
n12.handle(right) -> n13.handle(left) [label="Yes"]
n12.handle(bottom) -> n14.handle(top) [label="No"]
n13.handle(bottom) -> EventLog.n15.handle(top) [label="Persist"]
n14.handle(top) -> OrderSystem.n4.handle(bottom) [label="Error"]
}
EventLog { # Event Log
n15: rectangle label:"Record state transition"
n16: rectangle label:"Emit state changed event"
n17: diamond label:"Final state?"
n18: rectangle label:"Trigger completion handlers"
n19: rectangle label:"Continue processing"
n15.handle(right) -> n16.handle(left)
n16.handle(right) -> n17.handle(left)
n17.handle(right) -> n18.handle(left) [label="Yes"]
n17.handle(bottom) -> n19.handle(top) [label="No"]
n18.handle(top) -> OrderSystem.n4.handle(bottom) [label="Complete"]
n19.handle(top) -> OrderSystem.n4.handle(bottom) [label="Updated"]
}