State Transition
State Transition
This modeling technique came from a more formal area called automata theory.
State transition diagram depicted a Finite State Machine.
Software Program View
2. The change made to the values of the variables through assignment statements
represent a transition of state
A very simple example
light switch (page 368 of your text)
From To
Alt State Event State
(light) (switch) (light)
switch light
[l_st=on] on turnOff off
TurnOff ( )
turnOff
light light
on off
turnOn
3. “State transition diagram”
for light with switch events
A little “more” on the light switch
turnOff
turnOff turnOff
on off
on off
Telephone
initial state
off hook
Idle Active state
on hook
transition event
Transition Actions and Guards
• A transition can cause an action to fire.
– In software implementation, a method of the
class of the state machine is invoked.
• A transition may have a conditional guard.
– The transition occurs only if the test passes.
Fig. 29.2 Transition action and guard notation
transition action
on hook
guard condition
Nested States
• A state may be represented as nested
substates.
– In UML, substates are shown by nesting them
in a superstate box.
• A substate inherits the transitions of its
superstate.
– Allows succinct state machine diagrams.
Fig. 29.3 Nested states
Active
[valid subscriber]
Idle
PlayingDialTone Talking
on h
o ok
complete
Dialing Connecting
State-Independent vs. State-Dependent
• State-independent (modeless) — type of
object that always responds the same way
to an event.
• State-dependent (modal) — type of object
that reacts differently to events depending
on its state or mode.
Use state machine diagrams for modeling
state-dependent objects with complex
behavior, or to model legal sequences of
operations.
Modeling State-dependent Objects
• Complex reactive objects
– Physical devices controlled by software
• E.g., phone, microwave oven, thermostat
– Transactions and related business objects
• Protocols and legal sequences
– Communication protocols (e.g., TCP)
– UI page/window flow or navigation
– UI flow controllers or sessions
– Use case system operations
Fig. 29.4 Web page navigation modeling
Fig. 29.5 Legal sequence of use case operations
Process Sale
enterItem
endSale
makeCreditPayment
AuthorizingPayment makeCheckPayment
GoF State Pattern
• Problem:
– An object’s behavior is dependent on its state,
and its methods contain case logic reflecting
conditional state-dependent actions.
• Solution:
– Create a state class for each state,
implementing a common interface.
– Delegate state-dependent operations from the
context object to its current state object.
– Ensure context object always points to a state
object reflecting its current state.
Example: Transactional States
• A transactional support system typically
keeps track of the state of each persistent
object.
– Modifying a persistent object does not cause
an immediate database update — an explicit
commit operation must be performed.
– A delete or save causes change of state, not
an immediate database delete or save.
– A commit operation updates the database if
an object was modified (“dirty”), but does
nothing if the object is “clean”.
Fig. 38.12 Statechart for PersistentObject
save
commit / insert rollback / reload
New OldClean OldDirty
commit / update
delete
Legend:
New--newly created; not in DB delete
Old--retrieved from DB
Clean--unmodified OldDelete
rollback / reload
Dirty--modified
ProductSpecification Persistence
... PersistentObject
oid : OID
timeStamp: DateTime
commit()
delete()
rollback()
save()
...
Case-logic Structure
• Using case logic, commit and rollback
methods perform different actions, but
have a similar logic structure.
public void commit()
{
switch ( state )
{
case OLD_DIRTY:
// . . .
break;
case OLD_CLEAN:
// . . .
break;
. . .
State Transition Model using State Pattern
{ state.rollback( this ) }
{ state.commit( this ) }
{ state.save( this ) }
PersistentObject
{ // commit
PersistenceFacade.getInstance().update( obj )
obj.setState( OldCleanState.getInstance() ) }
{ // rollback
PersistenceFacade.getInstance().reload( obj )
obj.setState( OldCleanState.getInstance() ) } { // commit
PersistenceFacade.getInstance().insert( obj )
{ // delete obj.setState( OldCleanState.getInstance() ) }
obj.setState( OldDeleteState.getInstance() ) }
{ // commit
{ // save PersistenceFacade.getInstance().delete( obj )
obj.setState( OldDirtyState.getInstance() ) } obj.setState( DeletedState.getInstance() ) }