State support is increasingly a requirement as more and more software is squeezed into ever-smaller devices. Instead of writing reams of if-else code to distinguish between states, it’s often better to use the state design pattern.
In this article, I reused some code from an earlier article and modified it for a different application domain.
The state design pattern is sufficiently abstract to facilitate this type of cross-domain reuse, which is why this pattern should form a part of your programming toolkit. To support a new state, you just clone and modify an existing state.
The state pattern can be used effectively with a modest investment in coding. The source code for this article (see reference ) consists of five C++ header files and five accompanying C++ implementation files.
To build this code and try it for yourself, just create a Visual C++ console project, drop the files in, switch off precompiled headers, build, and test!