The State Pattern allows a complex system to be implemented in a structured way. The overall behaviour of a system is partitioned into well-defined states. Typically, each state is implemented by a class. The overall system behaviour can be determined firstly by knowing what state it is 'in'; secondly, by the behaviour embodied in the methods of the class corresponding to that state.
Here is an example:
Here is the output: