chapter thirteen

13 The State Design Pattern

 

This chapter covers

  • The State Design Pattern

In chapter 12, we saw how the Observer Design Pattern provides a model for an application where the subject object (the publisher) plays a key role in the operation of the application by providing content data for the observer objects (the subscribers). In this chapter, an object also plays a key role. We must monitor the runtime state changes of this object because its behavior, which depends on its current state, is critical to the operation of the application.

As we saw in section 4.5, events that change the values of the object’s instance variables can cause an object to make a transition from one state to another. The object can behave differently according to its current state. The State Design Pattern provides a model for managing an object’s states, the state transitions, and the different behaviors in each state.

Learning about design patterns

Be sure to read the introduction to Part 4 for important information about design patterns in general and to learn how this and subsequent chapters teach each pattern.

13.1 The State Design Pattern models state transitions

13.1.1 Desired design features

13.1.2 Before using State

13.1.3 After using State

13.1.4 State’s generic model

13.2 Choose between State and Visitor

13.3 Summary