You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: what-is-a-state-machine.md
+13-5Lines changed: 13 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
---
2
2
sitemap:
3
-
lastmod: 2019-07-08
4
-
priority: 0.4
5
-
description: A state machine is a machine that can be in exactly one state at a time, reacting to events by transitioning between states.
3
+
lastmod: 2020-04-10
4
+
priority: 0.7
5
+
description: A state machine is a way of modeling program state so that a program will always be in exactly one state at any one time.
6
6
keywords:
7
7
- state
8
8
- state machine
@@ -23,8 +23,6 @@ And further:
23
23
24
24
A state machine is _also_ a visual depiction of such an abstract machine.
25
25
26
-
Understanding state machines is almost the same as understanding **statecharts**. In many ways, statecharts are the "bigger brother" of state machines, designed to overcome some of the limitations of state machines.
27
-
28
26
## Example
29
27
30
28
Here is a visual depiction of a simple state machine. It is a model of a simple on/off switch.
@@ -39,8 +37,18 @@ Some observations over this machine.
39
37
* When the machine _enters_ the "on" state, a side effect occurs. A light is turned on.
40
38
* When the machine _exits_ the "on" state, another side effect occurs. A light is turned off.
41
39
40
+
This simple state machine is comparable to a boolean variable—which can either be true or false—that controls the _on_-ness of something.
41
+
42
+
## What is state anyway?
43
+
44
+
Program state is the _set of all variables in a program and their values at any point in time_ (see [Wikipedia](https://en.wikipedia.org/wiki/State_(computer_science)#Program_state)). A program or software component that has five independent variables, that each could be true or false, then it could in theory _be_ in any of 32 states (2 to the power of 5 = 32). However, a program will often have _invalid_ states, and in traditional software, the variables are carefully checked and manipulated in such a way that these invalid states don't happen.
45
+
46
+
A state machine is an alternative way of modeling program state: Instead of defining independent variables, a machine is crafted specifically to handle what states are possible, and when a machine is a given state, what _next_ state is allowed. Those five independent boolean variables are replaced with a single _state machine_ which intrinsically can only be in valid states.
47
+
42
48
## Relationship with statecharts
43
49
50
+
Understanding state machines is almost the same as understanding **statecharts**. In many ways, statecharts are the "bigger brother" of state machines, designed to overcome some of the limitations of state machines.
51
+
44
52
State machines are closely related to their bigger brother, _statecharts_. A statechart is essentially a state machine that allows any state to include _more_ machines, in a hierarchical fashion. This is to overcome some of the limitations that are inherent to state machines.
45
53
46
54
The primary goal of statecharts.github.io is to help you to understand statecharts. An understanding of state machines is a nice side effect. [What is a statechart?](what-is-a-statechart.html)
0 commit comments