Finite state machine and state mode

Source: Internet
Author: User

State Machine

Before understanding the state machine, the state is always simply understood as the state mode, recently, I carefully analyzed the state machine implementation mechanism, found that state machine and state mode is still very different.

One, The state pattern is specific , has a state set for each requirement, and implements a unique migration mechanism for it. state machine is abstract , not for specific needs, but for a variety of problems and related to the further abstraction, then use the state machine back to achieve state mode, only need to pay attention to the problem itself, not to care how to achieve, This means that you only need to draw a state migration diagram, and the state machine can help you achieve it.

Two, The state pattern is imperative , and we have to step through the process of how to migrate between States, and some additional things need to be done during the migration. state machine is declarative , using state machine, you only need to declare the state and state of the migration route, and do not need to provide the execution level of the command, the state machine automatically help you to do.

Three, the state mode is easy to understand, the state machine is really not so easy to understand, it is estimated that many people will think of me like, think of it no difference, it is wrong. (This is a difference, haha)

Distinguish so much, to the next, to analyze a typical state machine implementation, first briefly look at the class diagram:

First, the event, as the name implies is the meaning of the events, in the state of the migration process occurs, divided into the active event activeevent (corresponding to the original command), the passive event passiveevent.

An active event is an event that automatically sends information outside the state machine after it enters a state, and the passive event is an event that is accepted by the state machine to complete the state migration.

Second, Commandchannel, the command channel is used to receive the passive event and send the active event, by the controller holding, the specific requirements can achieve their own specific command channel.

        three, state, status, and state mode correspond to each specific state, the difference is that each state in the state pattern corresponds to a particular state class (imperative, valid information has a class name, a migration method), and here is a simple structure, The main include name, migration table (corresponding to the former class name and migration method), the Active event table (automatically called after entering the state), Auto-fire Event table After the active event call is complete, to The state machine itself "send passive event , with the use of a state that is just an interim state, will be automatically migrated to the next state, such as a door: open event causes the door to enter the opening (opening) state, and this state will do an action (the angle slowly expands), Then automatically migrates to the opened (already open) state, then opening has an open auto-firing event that sends the event to the state machine at the end of its processing to automatically switch to the opened state

Three, Transition, the state switch path, contains the EventCode (event source, which specifies the current state of interest in the event), source (Origin state), Trigger (trigger), Target (destination state).

Four, StateMachine, to the state machine, the state machine itself is very simple, including start (state machine's starting state), Allstates (all possible states of the state machine).

Five, controller, which is responsible for receiving external event handle (EventCode), directs the state machine to complete the windowsill migration, including CurrentState (state machine's current state), machine (state machines), Commandchannel ( Command channel).

The above introduces a simple class diagram, it is worth reminding that, from the static structure, the only place that can be extended is Commandchannel, this class has a Dosend method needs to be implemented, each concrete application implementation of this method to do the specific operation according to its state parameters.

From the source code point of view:

One, abstract events

Two, the active event

Three, passive events

Four, state

Five, state machine

VI, migration

Seven, Controller

Above is the detailed code, has the interest to read, I also was drunk (fellow person AH). Here I do two simple examples, are all about the door example of the first example is as follows: There are two states: on and off, in the open state to accept the close event, the migration to the off state, in the off state, accept the Open event, the migration to open state. The test is as follows:

The code is not "declarative" flavor of AH:).

The second example adds two states to this example: closing, opening, as follows:

The test code is as follows:

Bibliography "Domain-specific language"

Finite state machine and state mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.