liveBook · Manning

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.

How 2 Manning books explain it

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

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.