Programming with State Machines and the State Pattern

by Mark Mahoney

This is a paid playlist. Do you want access to it? It is available for sale for $3.00. Click here to purchase
access to the playlist

This is a brief introduction to tackling problems using state machines. Any system that must react to many different inputs based on a current 'state' can benefit from using state machines. There are many systems that typically have these requirements (embedded, UI, etc.). The first playback shows how to handle state based requirements using a simple nested 'if statement' approach. The second playback shows how to implement the same code using the State pattern from 'Design Patterns: Elements of Reusable Object-Oriented Software' by the Gang of Four. The third playback is a more complex state machine that is implemented with the State pattern. The programming language I use in these playbacks is Java but if you have some OO programming experience in other languages I believe you can still get value from them.
1. State Machines
In this playback I am going to write some code to split a string around the spaces inside it. This is helpful for user interfaces that ask a user to supply a series of values in a single textbox, for example, in a search bar. 

In Java, like most other languages, the string class has a built in method to split a string. However, in most search bars the user separates individual words. Sometimes the user wants to group two or more words together. In this case quotes are usually required to group the words together.

For example, if I were to search for some picnic menu items I might type in this in a search bar:

hamburgers chips 'hot dogs' pickles 'french fries'

The single quotes identify that some groups of words should be treated as a single unit. The grouping of words together means that a simple split on spaces can't be used. I will write a class to handle this specialized split that takes word groups into account.

This version of the program will not use the State pattern but should set the stage for using it in the next program.
This playback shows how to split a string around spaces if the string has some quotes in it. For example, the following string:

hamburgers chips 'hot dogs' pickles 'french fries'

should be split like this:

["hamburgers", "chips", "hot dogs", "pickles", "french fries"]

In a previous playback I showed how to break down this problem with a state machine and implement it with a series of if statements. This time I am showing how to implement a state machine using the State pattern. Converting a state machine into code using this pattern is a fairly mechanical and straightforward process.

The main benefit of using this approach compared to the last one is that this one is more readable and easy to reason about (especially for complicated state machines).
This program will count the number of comment characters and code characters in a C source code file. In classic C, there is only one type of comment, multi-line comments. These comments start with a /* and can span many lines until a */ is encountered.

This program uses a state machine to go through some code one character at a time to identify and count how much of it is code and how much of it is comments. It uses the State pattern to implement the state machine. The state machine is a little more complex than the string splitting examples I discussed previously.

This example comes from a book that got me really excited about state machines many years ago: Practical Statecharts in C/C++: Quantum Programming for Embedded Systems by Miro Samek.