If you need a simple synchronous state machine, where no more than one execution is executed at any given time, the model that I think of is as follows:
1) The execution context is represented by the Context object. The context is passed between states and is used for decision making by the thread manager. The context API depends on how versatile your system should be.
2) The State interface contains the execute (Context) method, where specific logic takes place. Permitted to use and modify context data.
3) The manager is configured with transition rules. He is able to determine the next state to execute, given the last state and context. It begins with the fulfillment of the initial state. After each execution of state S, it checks the context object for transition rules associated with state S. When it reaches the state of the terminal, the flow ends.
With this design, state implementations in no way know about the manager and are not involved in routing decisions.
source share