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 there are narrowly defined transitions to other states.
To make this work, we need a manager or context class that provides an interface for switching states. Internally, this class contains a pointer to the current state. Each state knows what other states it is allowed to be in and will transition to those states depending on the actions invoked upon it.
Here's how it looks in UML:
Figure 11.7: State pattern 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 current state object. The State objects are typically hidden from any other objects...