11.5 The State pattern
The State pattern is structurally similar to the Strategy pattern, but its intent and purpose are very different. The goal of the State pattern is to represent state transition systems: systems where an object’s behavior is constrained by the state it’s in, and some of the behavior includes transitions from state to state.
One way to make this work is to define a manager or context class that provides an interface for the various states. Internally, this class is a container for an object that represents the current state. A state object can change the state of the manager, transition to a different state object.
In the next page, we’ve shown how it looks in UML.
The State pattern decomposes the problem into two types of classes: the Core class and multiple State classes. The Core class maintains the current state, and forwards actions to a...