Changing Object Behavior with the State Pattern
The State pattern is usually associated with the idea of Finite State Machines (FSMs), a mathematical model of computation, but these are two different terms. While the FSM is a set of abstractions that help us create models for possible states in which a given machine can be, the State pattern is an Object-Oriented Programming (OOP) approach to abstract object states as objects. After these abstractions, you can implement an FSM to integrate these states and add transitions between one another. But just take note that the State pattern, besides being closely related to the FSM, is just the idea of encapsulating an object’s state into a new class and, with that, being able to change the object’s behavior in runtime.
Remember, an object’s state is just the set of values of the object’s properties, and by applying the State pattern, we explicitly give name and meaning to specific states in which an object...