State machine and State mode

Source: Internet
Author: User
Tags fsm

State machine is abbreviated as FSM (finite state Machine), mainly divided into 2 categories: finite state machine, (English: finite-state > Machine, FSM), also known as the finite automaton.
The first class, if the output is related to the state and not the input, is called the Moore State Machine
In the second category, the output is not only related to the state but also to the input, called the mealy state machine 1. Introduction

The state machine can be summed up into 4 elements, namely, present state, condition, action and secondary state. This generalization is mainly due to the consideration of the intrinsic causal relationship of the state machine. "Present state" and "condition" are the cause, "action" and "secondary state" is the fruit. Detailed details are as follows:
①: Refers to the current state of being.
② conditions: Also known as "events", when a condition is met, an action is triggered, or a state migration is performed.
③ Action: An action that is performed after a condition is satisfied. After the action is finished, you can migrate to a new state, or you can still keep the original state. The action is not required, and when the condition is satisfied, you can migrate directly to the new state without performing any action.
④: The new state to be moved after the condition is satisfied. The "second state" is relative to the "present state", and once it is activated, it turns into a new "present state".

The state mode allows an object to change its behavior as the internal state changes, and the object appears to modify his class.

State stores information about the past, which means that it reflects the input changes from the beginning of the system to the present moment. A transfer indicates a state change, and it is described by conditions that must satisfy the transfer to occur. An action is a description of the activity to be performed at a given moment. There are several types of actions:
Enter action (entry action): When entering a State
exit Action: When exiting State
input action: dependent on current state and input condition
transfer action: When a specific transfer is made

An FSM (finite state machine) can be represented using a state diagram (or state transition diagram) as shown in Figure 1 above. In addition, you can use several types of state transition tables. The most common representation is shown below: The combination of the current state (B) and condition (Y) indicates the next state (C). The full action information can be added using only footnotes. FSM definitions that include full action information can use state Table 2. State mode

Before using state mode, the client outside needs to intervene to change the state, while the implementation of state change is trivial or complex.
After using the state mode, the client can directly use event events to implement it without having to care about how the event causes state changes, which are implemented internally by the state machine.
This is a event-condition-state, and the state pattern encapsulates the condition-state part.
Each state forms a subclass, each state only cares about its next possible state, thereby virtually forming the rule of the state transition. If the new state is joined, only its previous state modifications and definitions are involved.

The main advantage of the state pattern is that it encapsulates the transformation rules, and enumerates the possible states, which put all the behavior associated with a state into a class, and can easily add new states, changing the behavior of objects by changing the state of objects, and allowing multiple environment objects to share a state object. In order to reduce the number of objects in the system; The disadvantage is that the use of State mode will increase the number of system classes and objects, and the structure and implementation of state patterns are more complex, if the use of improper will lead to the structure of the program and code confusion, for the state mode can be switched to not meet the "open-closing principle" requirements.

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.