The state design pattern
The state design pattern is really similar to the strategy design pattern that we looked at in the previous chapter.
Note
The purpose of the state design pattern is to allow us to choose a different behavior of an object based on the object's internal state.
Basically, the difference between the state design pattern and the strategy design pattern comes from the following two points:
- The strategy design pattern is about how an action is performed. It is usually an algorithm that produces the same results as other algorithms.
- The state design pattern is about what action is performed. Depending on the state, an object could be doing different things.
Implementing the state design pattern also closely resembles the implementation of the strategy design pattern.
Example class diagram
Imagine a media player. Most media players have a play button—when we activate it, it usually changes its appearance and becomes a pause button. Clicking the pause button now also does something...