Transition methods in the state design scheme

I have a state machine with many states A--B--C--D--E I have many transitions from C , for example, to A , if any condition is checked. For each state, I have a class extending the abstract class State , and I have a manager that delegates each method to the state method. The question is, can direct manager transition methods be indicated? ". I saw on the Internet only examples in which there is a main class that knows exactly how many times the transition occurs (i.e. insertQuarter() , ejectQuarter() , turnCrank() , dispense() ). The only way I found it is to call the manager’s state transition methods. Is it wrong or bad practice?

Thanks in advance Tobia

+4
source share
2 answers

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.

+3
source

Yes ... At least if I understood your question correctly. The manager must maintain a link to the current state, so the current state should be able to ask the manager to go to the next current state. Take a look at the Wikipedia example for an example.

0
source

Source: https://habr.com/ru/post/1312444/


All Articles