We use the State design pattern to implement a system that will permit an object to change its behavior based on its internal state. Thus, a change of context will bring on a change of behavior.
The State pattern has three core participants in its structure:
- The Context class defines an interface that permits a client to request a change in the internal state of an object. It also holds a pointer to the current state.
- The IState interface establishes an implementation contract for the concrete state classes.
- The ConcreteState classes implement the IState interface and expose a public method named handle() that the Context object can call to trigger the state's behavior.
Let's now review a diagram of this pattern definition, but in the context of an actual implementation:
Figure 5.1 – UML diagram of the State pattern
To update an object's state, the client can set the expected state through the Context...