In what areas of programming would I use state machines ? Why ? How could I implement one ?
EDIT: please provide a practical example , if it's not too much to ask .
State Machines are used in applications where distinguishable states exist. Each state can lead to one or multiple states and can also end the process flow. A State Machine relies on user input or in-state calculation to determine which state to go to next.
Conclusion. State machines are useful if you can define a clear number of states and events that will trigger transitions to them. They might be good for user interfaces or communications protocols. However, when building complex systems, implementation of state machines is not the best option.
Finite state machines are important because they allow us to explore the theory of computation. They help us discover what resources are needed to compute particular types of problem. In particular finite state machines are deeply connected with the idea of grammars and languages that follow rules.
Finite state machines can be used to model problems in many fields including mathematics, artificial intelligence, games, and linguistics. A system where particular inputs cause particular changes in state can be represented using finite state machines.
Use a state machine to represent a (real or logical) object that can exist in a limited number of conditions ("states") and progresses from one state to the next according to a fixed set of rules.
A state machine is often a very compact way to represent a set of complex rules and conditions, and to process various inputs. You'll see state machines in embedded devices that have limited memory. Implemented well, a state machine is self-documenting because each logical state represents a physical condition. A state machine can be embodied in a tiny amount of code in comparison to its procedural equivalent and runs extremely efficiently. Moreover, the rules that govern state changes can often be stored as data in a table, providing a compact representation that can be easily maintained.
Trivial example:
enum states { // Define the states in the state machine. NO_PIZZA, // Exit state machine. COUNT_PEOPLE, // Ask user for # of people. COUNT_SLICES, // Ask user for # slices. SERVE_PIZZA, // Validate and serve. EAT_PIZZA // Task is complete. } STATE; STATE state = COUNT_PEOPLE; int nPeople, nSlices, nSlicesPerPerson; // Serve slices of pizza to people, so that each person gets /// the same number of slices. while (state != NO_PIZZA) { switch (state) { case COUNT_PEOPLE: if (promptForPeople(&nPeople)) // If input is valid.. state = COUNT_SLICES; // .. go to next state.. break; // .. else remain in this state. case COUNT_SLICES: if (promptForSlices(&nSlices)) state = SERVE_PIZZA; break; case SERVE_PIZZA: if (nSlices % nPeople != 0) // Can't divide the pizza evenly. { getMorePizzaOrFriends(); // Do something about it. state = COUNT_PEOPLE; // Start over. } else { nSlicesPerPerson = nSlices/nPeople; state = EAT_PIZZA; } break; case EAT_PIZZA: // etc... state = NO_PIZZA; // Exit the state machine. break; } // switch } // while
Notes:
The example uses a switch()
with explicit case
/break
states for simplicity. In practice, a case
will often "fall through" to the next state.
For ease of maintaining a large state machine, the work done in each case
can be encapsulated in a "worker" function. Get any input at the top of the while()
, pass it to the worker function, and check the return value of the worker to compute the next state.
For compactness, the entire switch()
can be replaced with an array of function pointers. Each state is embodied by a function whose return value is a pointer to the next state. Warning: This can either simplify the state machine or render it totally unmaintainable, so consider the implementation carefully!
An embedded device may be implemented as a state machine that exits only on a catastrophic error, after which it performs a hard reset and re-enters the state machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With