By: S. Yacoub, H. Anmar
Published in: PLoPD4
Category: Finite State Machines
Summary: An extension of State Patterns and Three-Level FSM to implement a state machine in an object-oriented design.
An extension of State Patterns and Three-Level FSM to implement a state machine in an object-oriented design.
An object's state changes in response to events in the system. To implement the correct object behavior, use State [Gamma+95] and add one of the state transition mechanisms of State Patterns
To get a state object to change when the owning object's state changes, have the state object initiate the transition from itself (the current state) to the new state object. This ensures that transitions are atomic and removes state-dependent code.
To enable communication between entities when the behavior of one is described by an FSM, encapsulate the state classes and state-transition logic in the machine, and provide a simple interface that receives events.
You're using an FSM. To make your design maintainable, structure the system in layers that decouple the logic of state transition from object behavior.
To begin FSM output, make the concrete event method of each state call the output action method.
To begin FSM output produced only at state entry when each state has a specific set of outputs, implement an output method in each state that calls the required actions. Make the state transition mechanism call the output method of the next upcoming state.
If some FSM outputs are started on events and other outputs are started only in a given state, make the event method of each state produce the event-dependent outputs, and make the state transition mechanism call an output method of the upcoming state to produce the state-dependent output.
Your FSM should follow a sequence of state changes. To ensure that no other state changes are imposed, encapsulate the current state and keep the reference private.
Your application is large, with too many states. To instantiate them, don't initially create all states. Make each state knowledgeable of upcoming states, and create instances of them on state entry and delete them on state exit.