UML State Machine Diagrams and Modeling
UML State Machine Diagrams and Modeling
State Machine Diagram Illustrates the interesting events and states of an object and the behavior of an object in reaction to an event.
Event: significant or noteworthy occurrence.
E.g., telephone receiver taken off hook.
State: the condition of an object at a moment in time (between events). Transition: a relationship between two states; when an event occurs, the object moves from the current state to a related state.
UML State Machine Diagram States shown as rounded rectangles. Transitions shown as arrows. Events shown as labels on transition arrows. Initial pseudo-state automatically transitions to a particular state on object instantiation. Events with no corresponding transitions are ignored.
initial state
state
transition
event
transition action
off hook / play dial tone Idle [valid subscriber] on hook guard condition Active
Active
Talking
on h ook
digit
digit
connected 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.
WatingForSale
makeNewSale
EnteringItems enterItem
endSale
authorized
makeCashPayment
WaitingForPayment
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.
New
OldClean
OldDirty
delete Legend: New--newly created; not in DB Old--retrieved from DB Clean--unmodified Dirty--modified Deleted delete rollback / reload OldDelete
commit / delete
Assume all persistent object classes extend a PersistentObject class that provides common technical services for persistence.
Domain 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.delete( this ) }
{ state.rollback( this ) }
PersistentObject oid : OID state : PObjectState commit() delete() rollback() save() setState(PObjectState) ... PObjectState { commit(obj : PersistentObject) delete(obj : PersistentObject) rollback(obj : PersistentObject) save(obj : PersistentObject) // default no-op // bodies for // each method }
OldDirty State Product Specification ... ... ... ... commit(...) delete(...) rollback(...) Sale
OldClean State
New State
OldDelete State
delete(...) save(...)
commit(...)
commit(...) rollback(...)
{ // commit PersistenceFacade.getInstance().update( obj ) obj.setState( OldCleanState.getInstance() ) } { // rollback PersistenceFacade.getInstance().reload( obj ) obj.setState( OldCleanState.getInstance() ) } { // delete obj.setState( OldDeleteState.getInstance() ) { // save obj.setState( OldDirtyState.getInstance() )
{ // commit PersistenceFacade.getInstance().insert( obj ) obj.setState( OldCleanState.getInstance() ) } { // commit PersistenceFacade.getInstance().delete( obj ) obj.setState( DeletedState.getInstance() ) }