What is a finite state machine?

directory

1. Background

2. Knowledge analysis

3. Frequently Asked Questions

4. Solutions

5. Coding

6. Expand your thinking

7. References

8. More discussion

1. Background What is a finite state machine? Finite-state machine (FSM), also known as finite-state automaton, abbreviated as state machine,

Is a mathematical model that represents a finite number of states and behaviors such as transitions and actions between these states.Copy the code

Its main function is to describe the sequence of states experienced by the object in its life cycle and how to respond to various events from the outside world. In computer science,

Finite-state machines are widely used in modeling application behavior, hardware circuit system design, software engineering, compilers, network protocols, and computing and language research.Copy the code

TCP state machine

Let’s take a look at Ruan Yifeng’s description of finite state machines

It has three characteristics:

The total number of states is finite.

In one state at any one time.

Under certain conditions, they transition from one state to another.

What it means for JavaScript is that many objects can be written as finite state machines.

For example, a web page has a menu element. When the mouse hover, the menu is displayed. The menu is hidden when the mouse is moved away.

If the finite state machine description is used, the menu has only two states (show and hide), and the mouse triggers a state transition.

var menu = {

// Current status

CurrentState: ‘hide’,

// Bind events

The initialize: function () {

Var self = this;

Self. On (” hover “, the self. The transition).

},

// State transition

The transition function (event) {

The switch (enclosing currentState) {

Case “hide” :

Enclosing currentState = ‘show’;

DoSomething ();

Break;

A case of “show” :

Enclosing currentState = ‘hide’;

DoSomething ();

Break;

Default:

The console. The log (‘ Invalid State! ‘);

Break;

}

}

};

But what we really need is just a state transition, mouse up to show menu, otherwise hide,

The code above is inefficient and unwieldy, complicating a simple problem.

So if-else works fine when the problem is small, but when the problem is large,

Introducing the concept of a state machine can be very useful when either a large number of IF-Else or the solution itself is too complex to maintain.Copy the code

And when we hand over the implementation of the state machine to a javascript-state-machine library, we are really only concerned with the solution itself, rather than implementing the state machine ourselves.

When we focus only on the solution itself, the problem is undoubtedly easier to solve.Copy the code

How do I use the javascript-state-machine library

4. The solution introduces the JS file state-machine

How do you decide when or how to use a finite state machine

7. References Reference I :javascript-state-machine

Reference two: Ruan Yifeng’s network log

More discussion Thanks for watching

Today’s share is over here, welcome everyone to like, forward, leave a message, pat brick ~

Skill tree.IT Monastery

“We believe that everyone can become an engineer. From now on, find a senior to guide you to the entrance, control the pace of your own learning, and no longer feel confused on the way to learning.”

Here is the skill tree.IT Shuzhen Institute, where thousands of brothers find their own learning route, learning transparent, growth visible, brothers 1 to 1 free guidance.

Come with me learn ~ http://www.jnshu.com/login/1/21109035