Finite-state machine is a mathematical calculation model that represents a finite number of states, as well as the transfer and action behaviors between these states. It is widely used in modeling application behavior, hardware circuit system design, software engineering, compiler, network protocol, and computing and language research.

The emergence of state machines is to model the state changes of complex objects, and to deal with them in an engineering way, so as to facilitate understanding and communication

State machine introduction

In actual development, the problem of state machines is often encountered. Take a simple example, consider the order status in the user payment scenario. The order status may change as follows: To be paid > In process > Failed/succeeded In this process, the business system needs to query the order status in the third-party payment system (wechat, Alipay) and update the order status in the local library.

There are many similar scenarios, especially when the object state is more and more, it becomes more and more difficult to maintain, but also accompanied by this concurrency problem, a careless will be wrong. Therefore, it is necessary to design a general purpose state machine manager.

State machine design

A good state machine design, first of all, should have good versatility, can adapt to many scenarios, secondly, friendly to developers, developers do not care about the internal implementation.

According to the definition of a state machine, a state machine is composed of state transitions. Each state transition consists of a pre-state, a behavioral operation, and a post-state. Usually, the state change is accompanied by an event notification.

State machine implementation

The implementation of a state machine is not difficult, and consists of the following classes

  • StateManager: StateManager, implementer of the main logic of the state machine, itself an abstract class that maintains a set of automatic state transitions (non-final state -> final state) and a set of state change listeners
  • StateTransition: StateTransition, which defines preState, operation, and post-state.
  • StateListener: state change listener
  • StateAutoSync: registers all state managers internally and is responsible for finding the corresponding state manager for state synchronization

StateManager subclasses implement two main classes LocalStateManager (memory maintenance state) and GlobalStateManager (global storage maintenance state, such as mysql/ Redis)

Asynchronous event-driven

Added event-driven support for asynchronous (thread pool concurrency) and synchronous modes

The whole process is roughly as follows: the processing request enters the system as an Event, and the AsyncDispatcher is responsible for invoking the corresponding Event Handler. After the event scheduler finishes processing, a new event may be generated and distributed by the scheduler, or the scheduler itself may be a finite state machine, which handles the state transition internally.

For details, see: github.com/VectorJin/S…