A fundamental feature of virtually all design patterns is their ability to expand and accept change. The kind of change you're expecting in an application determines, to some extent, the type of design pattern you select. In this particular application, we're adding states.
The first state to be added to the state machine is a Pause state. This state only exists inside the Play state, and you cannot get to the Pause state directly from the Stop state. To get to the Pause state, you must first be in the Play state, and then you can turn the Pause state on and off. To correctly depict this new state, we need to use a hierarchical state diagram. Figure 10-5 shows a statechart with the necessary hierarchy.
Figure 10-5. Hierarchical statechart
The hierarchy in Figure 10-5 is a simple one. The first level is the Play and Stop states, and then, within the Play state are the Pause and No Pause states.
Because the pause function is a toggle between the Play and Pause states, the No Pause state is exactly the same as the Play state. So, rather than creating Pause Start and Pause Stop functions, a "Do Pause" behavior will be established to act differently in different states. In a Pause state, the Do Pause behavior returns to the default Play state and in the Play state, it goes to the Pause state.