State design pattern
The State design pattern encapsulates state-specific behavior in separate representations and delegates an object's behavior according to its current state.
Object-Oriented Software Design in C++
A behavioral design pattern that encapsulates state-specific behavior in separate objects and delegates behavior according to an object's current state.
- How it is used
- Models objects whose runtime behavior and allowed transitions depend on their current state.
- How deep it goes
- Explains centralized conditionals, state subclasses, polymorphic delegation, shared data, and returned transition objects.
- Examples
- Uses an automatic ticket machine with card validation, ticket purchase, invalid actions, and sold-out behavior.
- What you can do after
- Build it from scratch by separating state-specific actions into polymorphic classes and returning objects for state transitions.
Read how Object-Oriented Software Design in C++ explains state design pattern →
Software Design for Python Programmers
The State Design Pattern is a behavioral design pattern that allows an object to alter its behavior when its internal state changes by delegating state-specific behavior to separate state representations.
- How it is used
- Manages runtime states when an object's behavior and transitions depend critically on its current state.
- How deep it goes
- Explains the pattern through a conditional implementation, a polymorphic refactoring, a generic model, and a pattern comparison.
- Examples
- Uses an automatic ticket machine with READY, VALIDATING, TICKET_SOLD, and SOLD_OUT states.
- What you can do after
- Build it from scratch by separating state classes, delegating actions, and returning objects representing the next state.
Read how Software Design for Python Programmers explains state design pattern →
Related concepts
- design patternkind of · A design pattern is a reusable, general solution structure for a recurring software design problem in a particular context.
- state transitionuses · A state transition is a change from one modeled state of a system, object, or process to another according to an event, action, condition, or rule.
- stateuses · State is information or a configuration describing the condition of a system, entity, or computation at a particular time and potentially influencing subsequent behavior or decisions.
- behavioruses · Behavior is the externally observable actions, responses, outputs, or effects of a system or object under specified conditions or interactions.
- visitor design patterncontrasts with · The Visitor pattern separates operations on an object structure into visitor objects, allowing operations to be added without changing the structure’s element classes.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

