1
A Student Guide to Object-A Student Guide to Object-Oriented DevelopmentOriented Development
Chapter 7 State Diagrams
2
State diagram
In sequence diagrams you are looking at all the classes affected by one use case
In state diagrams you are looking at one class across all use cases and how it is effected by the events in the system
3
State diagrams are increasingly widely used to model the ways that a system can respond to external events
All objects of the class (the individual customers or products) have the same range of ways in which they can behave, but the actual way an individual object does behave during the running of the system depends on the sequence of events that it experiences.
State diagrams look at how a class is affected by the different use cases in the system and how the objects of the class behave in response to events that affect them. They model the behaviour of one object across several use cases.
State diagramState diagram
4
State DiagramState Diagram A state diagram only ever describes the behaviour of a single class
illustrating how its objects behave during their lifetimes and how they react to all uses cases in which they are involved.
State diagrams also model the order that events affect an object in e.g. in the Wheels system the bike must be assigned a number before it can be hired
In most systems only a few of the classes are complicated enough to merit a state diagram. These are classes where the response of an object of the class to a particular event varies depending on the state the object is in at the time
State diagrams are also referred to as state charts and state transition diagrams
5
States and EventsStates and Events
State - The state of the object here refers to the situation it is in while satisfying some condition (such as a bank account having some money) or waiting for an event (such as someone trying to withdraw or deposit money).
Event - An event is something that happens which has significance for the system and affects an object of at least one of the system’s classes. We can tell if an object is in a particular state by looking at the values of some of its attributes and its links to other objects.
6
The state of an objectThe state of an object
We can tell if an object is in a particular state by looking at the values of its attributes For example, if a Bank Account object is in credit the value of the balance attribute will be a positive amount or zero, but if it is overdrawn the value of balance will be negative.
In the Wheels case study, we can tell if a bike is hired out because there will be a link from the Bike object to an active Hire object.
7
start state
stop state
transition from one state to another
self-transition (no change of state)
event [guard] / action transition label (each of the three parts is optional)
state
8
state diagram for a bank account class
State diagram for a bank account class
In credit
Overdrawn
open account and deposit money
deposit money
withdraw money [new balance < 0 and within overdraft limit]
deposit money [new balance >= 0]
deposit money [new balance < 0 and within overdraft limit]
withdraw money [new balance >= 0]
withdraw balance and close account
withdraw money [new balance < 0 and within overdraft limit]
Start state
stop state
Event fires a transition
Self transition
Guard
In the "In credit" stateIn the "In credit" state
9
UML and C++ A Practical Guide To Object-Oriented Development
State ModelState ModelState Model describes the sequence of
states that an object goes through during its lifetime in response to events.
Includes the objects responses to events– Actions
• Single act performed by the object
– Activities• Continuous set of actions performed by an object
UML and C++ A Practical Guide To Object-Oriented Development
StateState
This represents a stage in the lifecycle of an object.
Technically the period of time during which an object is waiting for an event to occur.
The internal state of an object is the combination of the data values of the attributes of object.
UML and C++ A Practical Guide To Object-Oriented Development
EventEvent
An event is a condition that can be detected by an object.
From an programmatic perspective– An event is a one way asynchronous
transmission of information from one object to another.
– The information may have parameters with names and types as part of the message sent
UML and C++ A Practical Guide To Object-Oriented Development
TransitionTransition
A transition is a response by an object to an event received by it. – The response can be
• a change to a new object state
• a change to the same object state
• the performance of some action
The ability of the object to respond to a given event is described by guard conditions.
UML and C++ A Practical Guide To Object-Oriented Development
ActionAction
An action is an activity or operation that is done inside a state or on a transition between states
An action is– atomic– instantaneous
An action can occur– during a transition– on entry to a state, – during the entire period an object is in a state,– on exit from a state,– on arrival of an event that does not cause a state
transition
UML and C++ A Practical Guide To Object-Oriented Development
ActivityActivity
An activity is an operation or set of operations that executes during the entire time an object is in a state.
An activity is not atomic.
An activity can be interrupted by an event while it is executing
Events for Bike ObjectEvents for Bike Object
16
Completed state diagram for Bike Completed state diagram for Bike ObjectObject
17
18
State diagrams – key wordsState diagrams – key words
‘after’ – used to show an event taking place after a specified time period e.g. after [6 months] / delete
‘when’ – event takes place when a condition is satisfied e.g. when [all items in stock] / fill order
19
State diagrams – entry and exit eventsState diagrams – entry and exit events
offHook
entry / start dialling toneexit / stop dialling tonedo / emit dialling tone
Keyword ‘do’ – behaviour that lasts for the duration of a state is called an activity, it is modelled using the keyword ‘do’
UML and C++ A Practical Guide To Object-Oriented Development
Nested State DiagramNested State Diagram
Simple State
Complex State
Substate1
entry: entry action
Substate2
Substate1
entry: entry action
Substate2
event( args )[ cond ] / action t̂arget.event(args)event( args )[ cond ] /
action t̂arget.event(args)
event( args )[ cond ] / action t̂arget.event(args)
event( args )[ cond ] / action t̂arget.event(args)
UML and C++ A Practical Guide To Object-Oriented Development
Nested Concurrent SubstatesNested Concurrent Substates
filled out
Course registration request
partially filled out
SSN empty
validatingentry / check SSN
userentersSSN
SSNvalid
SSN notvalid /clear SSN
Course id empty
validatingentry / check cid
userenterscid
cidvalid
cid notvalid /clear cid
emptyentry / clear SSN and course id This state will not
be entered untilboth the SSN andCourse id fields arefilled in and valid
UML and C++ A Practical Guide To Object-Oriented Development
Nested Concurrent SubstatesNested Concurrent Substates
waiting for one field to be filled in
SSN empty validatingentry / check SSN
user enters SSN
SSN valid
HRid valid
HRid not valid /clear HRid
SSN not valid/ clear SSN
HRid empty validatingentry / check HRid
user enters HRid
filled out
emptyentry / clear SSN and HRid
UML and C++ A Practical Guide To Object-Oriented Development
Mealy model for Microwave OvenMealy model for Microwave Oven
UML and C++ A Practical Guide To Object-Oriented Development
Moore Moore model model