Chapter 10. The State Design Pattern
Could the young but realize how soon they will become mere walking bundles of habits, they would give more heed to their conduct while in the plastic state.
In each action we must look beyond the action at our past, present, and future state, and at others whom it affects, and see the relations of all those things. And then we shall be very cautious.
All our final decisions are made in a state of mind that is not going to last.
What Is the State Pattern?
The State design pattern is one of the most intriguing and useful patterns of those devised by GoF. Games often depend on the State pattern because objects can change states so frequently in games. The purpose of the State pattern is to allow an object to change its behavior when the state changes. Many other simulations, whether they are games or not, depend on the State pattern as well; you will find many uses for it, as this chapter demonstrates. Figure 10-1 shows the basic design pattern in a class diagram.
Figure 10-1. State design pattern class diagram
As shown, there’s no Client
class; however, GoF note that the Context
is the primary interface for the
clients. When examining the pattern, think of the Client
class making its request through the
Context
class. There’s a lot to understanding and effectively using the State design pattern, so for now, just ...
Get Learning PHP Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.