State Design Pattern
Behavioral pattern. Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
The State design pattern solves problems like: How can an object alter its behavior when its internal state changes?
Open in the interactive referenceDownload the PDF reference
Applicability
Design Problems
-
Defining State-Specific Behavior
- How can an object alter its behavior when its internal state changes?
-
How can state-specific behavior be defined
so that new states can be added and the behavior of existing states can be changed independently? - How can conditional statements that depend on an object's internal state be avoided?
Refactoring Problems
-
Complicated Code
- How can conditional statements that depend on an object's internal state be eliminated? Replace State-Altering Conditionals with State (166) [JK05]
Not included on this page: Intent, Problem, Solution, Motivation, Structure, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.