A state machine is an implementation of the mathematical notion of finite state automata. In a finite state automata an arbitrary set of input symbols is read sequentially. The automata consists of individual states and the state changes each time a symbol is processed. The number of possible states is finite and the change of states is known as a transition. The new state is determined by evaluating a state transition function:
new state = F(current state, current symbol)
Note that the new state is solely determined by the identity of the current state and the input symbol. Since the input symbols are arbitrary, the new state is also arbitrary once one is in a given state. Similarly, there is no way to deduce what the previous state was based solely on being in the current state. (BTW, there is nothing to prevent the new state from being the current state in
While this formalism provides a sound basis, it is not directly very useful for software development unless something else happens besides transitioning from one state to another. So for software we elaborate a bit on the notion of finite state automata be introducing the notion of a state action that does something when one transitions into a new states. So in a software context a state machine is a finite state automata with the following elements.
State. A state is a condition where some set of rules and policies apply. In an OO context those rules and policies will be extracted from the problem space and they represent intrinsic behavior responsibilities of the underlying problem space entity.
Transition. A transition is a valid change from one state to another. In general one cannot transition from the current state to any arbitrary state; typically only one or a few state changes are valid. In an OO context the valid transitions represent constraints on the sequence in which various state rules and policies may be executed during the life of the object.
Event. An event is equivalent to an input symbol in a finite state automata. In an OO context an event is a message with a specify identity and, optionally, a packet of data that will be processed by the state action. Only the event identity is relevant to evaluating the state transition function.
Action. The state action executes the rules and policies that are appropriate for the state whenever there is a transition into the state. That is true even if the previous state happened to be the same. (Transitions back to the same state are known as reflexive tranitions.) In the OO context a state action is a method.
In an OO context we map an entity's behavior responsibilities into a single object state machine during the process of abstraction. That object state machine is known as the object life cycle. Each state action represents a method that encapsulates a single, logically indivisible behavior responsibility at the level of abstraction of the object's class.
In an OO context the events that trigger transitions are almost always generated outside the receiving state machine. Those events represent collaborations with other objects in the subsystem or external stimuli.