Programming with State Machines and the State Pattern


by Mark Mahoney


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.