SWEN30006
Software Design and Modelling
UML State Machine Diagrams & Modelling
Textbook: Larman Chapter 29
“No, no, you’re not thinking, you’re just being logical.” —Niels Bohr
Learning Objectives
On completion of this topic you should be able to:
• Be aware of where state machine modelling is applicable.
• Understand UML state machine diagram notation.
4
(Revisited) Software Models
A static domain model (A domain class diagram)
A dynamic domain model (A system sequence diagram)
A dynamic design model (A design sequence diagram)
makePayment(cashTendered)
makePayment(cashTendered)
create(cashTendered)
A static design model (A design class diagram)
If an object (or a system) can have different behavior based on its status or condition, how can we model it?
: Register
: Sale
: Payment
5
State Machines
Definition: A state machine is a behavior model that captures the dynamic behavior of an object in terms of states, events, and state transitions.
• A state is the condition of an object at a moment in time
• An event is a significant or noteworthy occurrence that affects the object to change a
state
• A transition is a directed relationship between two states such that an event can cause the object to change from the prior state to the subsequent state
A visual model: UML State Machine Diagram
6
UML State Machine Diagram
Telephone
initial state
off hook Idle Pickup receiver
Active
state
Hang up
on hook
transition
event
7
Example: Partial State Machine Diagram for an iPhone
Side button
8
How to Apply State Machine Diagrams? (1)
Determines the behavior of an object
• State-dependent object:
– Reacts differently to events depending on the object’s state
• State-independent object:
– For all events of interest, an object always reacts to the event the same way
• State-independent w.r.t. an event:
– Always responds to event the same way
9
How to Apply State Machine Diagrams? (2)
Guideline 1: Consider state machines for state-dependent objects with complex behavior. – Model behavior of complex reactive objects
Guideline 2: Complex state-dependent classes are less common for business information systems, and more common in communications and control applications.
Example: Complex Reactive Objects
• Physical device controllers
• Transactions and related Business
Objects
• Role Mutators (objects that change
role)
Example: Protocols and Legal Sequences
• Communications Protocols
• UI Page/Window Flow, Navigation, or
Session
• Use Case Operation Sequencing
10
Example: Web Page Navigation
11
Example: Process Sale Operation Sequencing
Wa it in gFor Sale
Process Sale
m ake New Sale
En te ri ngI t em s en d Sa le
au th o ri zed
en te r It em Wa itingForPa yment
AuthorizingPayment
m ake Ca shP ay me nt
m ake Cr ed i tP ay men t
m ake Ch ec k Pay m en t makeStoreCardPayment
12
15
Transition Actions and Guards
• A transition action is an action (an object does something) when a transition happens – In a software implementation, this may represent the invocation of a method of the
class of the state machine diagram.
• A guard is a pre-condition to a transition, i.e., a transition won’t happen if the guard condition is false.
s s t t a a t te e s s
State A
A transition action
trigger [ guard ] / action
State B
When object is in State A:
if trigger event occurs and guard is true then
perform the behaviour action and transition object to State B.
A guard
transition
17
Example: Transition Action and Guard
Photography website
18
Exercise: Pedestrian Crossing Light
• The crossing light starts with the “Red Standing” light
• If the crossing button is pressed when the traffic light is “Red”, the crossing light
becomes “Green Walking” and a timer starts
• When a timer reaches 30 seconds, the crossing light becomes “Flashing Red Standing”
• When a timer reaches 60 seconds, the crossing
light becomes “Red Standing”
Nested States
A state allows nesting to contain substates; a substate inherits the transitions of its
superstate (the enclosing state)
Pickup receiver
A superstate
Active
Talking
connected
Idle
[valid subscriber]
PlayingDialTone
digit
digit Dialing
A substate
• Transition into Active (via Pickup receiver) transitions into substate PlayingDialTone
• All substates of Active inherit the hang up transition.
complete
Connecting
27
off hook / play dial tone
on hook
Choice Pseudostate
Choice pseudostate realizes a dynamic conditional branch. It evaluates the guards of the triggers of its outgoing transitions to select only one outgoing transition.
Entered PIN [PIN Correct] / Display Menu
Entering PIN
Entered PIN [PIN Incorrect] / Stun Patron
Selecting Transaction
Calling Police
Selecting Transaction
Calling 28 Police
Entering PIN
Entered PIN
[PIN Correct] / Display Menu
[PIN Incorrect] / Stun Patron
Choice Pseudostate (Continue)
Choice Pseudostate can
• Have two or more outgoing transitions
trigger
[bal < 0]
[0 <= bal < min]
[min <= bal]
State A
State B State C
• Use predefined [else] guard
– [else] outgoing transition chosen if no other guards are true
trigger
[day = Monday]
[else]
State A State B
29
Summary & Remarks
• A state machine is dynamic model that illustrates the behavior of an object in terms of states, events, and state transitions
• A state machine should be used if an object has complex behavior that depends on states or conditions
• In the workshop 6 supplementary materials, you will learn how to implement state machines
30
Lecture Identification
Lecturer: Philip Dart
Semester 1, 2021
© University of Melbourne 2021
These slides include materials from:
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, Third Edition, by Craig Larman, Pearson Education Inc., 2005.