Email this Mix
Slide 1 - State Diagrams
- Emerson Murphy-Hill
- Creative Commons Attribution 4.0 License.
- Material Produced by NCSU Software Engineering Faculty.
Slide 2 - Scenario
- From UML Distilled (pp. 107-108):
- “… controller for a secret panel in a Gothic castle. In this castle, I want to keep my valuables in a safe that’s hard to find.”
- “…to reveal the lock to the safe, I have to remove a strategic candle from its holder, but this will reveal the lock only while the [safe’s] door is closed. Once I can see the lock, I can insert my key to open the safe. For extra safety, I make sure that I can open the safe only if I replace the candle first. If a thief neglects this precaution, I’ll unleash a nasty monster to devour him.”
- States of the safe? Transitions (actions) of the owner/thief?
Slide 3 - State Diagram
- A state diagram (also called state machine diagram) depict the various states that an object may be in and the transitions between those states.
- Appropriate to be developed for complex objects.
Slide 4 - States
- States are represented by the values of the attributes or data members of an object.
- Initial state
- Terminal state
Slide 5 - Transitions
- Transitions are the result of the invocation of a method that causes an important change in state.
- Each transition has a label that comes in three parts. All the parts are optional.
- trigger-signature [guard]/activity
- candle removed [door closed]/reveal lock
- The trigger-signature is usually a single event that triggers a potential change of state.
- Missing trigger-signature [rare] – you take the transition immediately.
- The guard, if present, is a Boolean condition that must be true for the transition to be taken.
- Missing guard – always take the transition.
- The activity is some behavior that’s “executed” during the transition.
- Missing activity – don’t do anything during the transition.
- trigger-signature: event that causes a potential change of state
- guard: Boolean condition that must be true for transition to happen
- activity: behavior that’s executed during the transition
- Example Transition
Slide 7 - Rules for State Diagrams
- There is one initial state (can be multiple final states).
- Every state can be reached from the initial state.
- From each state, there must be a path to a final state.
- Every transition between states must be labeled with an event that will cause that transition.
- When an event occurs, you can take only one transition. If you have multiple transitions with the same event, the guards must be mutually exclusive.
- Transitions that are not shown are illegal OR show transitions that cause errors.