Recommended Free Tools
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use the GoF State pattern when an object’s lifecycle changes which behaviors are valid and those behaviors are becoming scattered across conditionals. For a small, stable set of states, an enum with conditional or match logic is often clearer; for a regular transition graph with little state-specific behavior, a transition table may fit better. The choice depends on the behavior and transition structure—not a universal state-count threshold.
What the GoF State pattern is for
In the State pattern, a context delegates state-dependent operations to an object representing its current state. The context remains the interface callers use; its behavior changes as the active state changes. This is useful when lifecycle rules affect several operations and conditional branches are becoming difficult to maintain.
For example, a long-lived business entity such as an account may allow different operations before and after it is closed. A state machine can make those lifecycle-dependent rules explicit. But first ask whether a state machine is needed at all: PMI’s Disciplined Agile reference warns, “State machines are not required nearly as often as they are used so the use of this pattern should be a yellow flag.” Treat that as a prompt to evaluate the design, not as a statistic.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWhich representation fits the problem?
GoF State is one way to represent a state machine, not a requirement of state-machine design. MIT course material distinguishes a machine represented as a class or object from states represented as objects or enumerations. The State-as-object form is useful in particular circumstances and can be clumsy when it does not fit.
#1 Best Overall
| Situation | Good starting point | Why it may fit |
|---|---|---|
| A few stable states, with short, local behavior differences | Boolean or enum with conditionals or match logic | It keeps the logic in one place with less indirection. Godot’s version 3.3 documentation explicitly presents enum-and-match logic as an alternative. |
| Several states with substantial, distinct behavior across multiple operations | GoF State objects | Each state can group its behavior, while the context delegates to the active state. |
| A large, regular graph whose transitions are mostly data and whose states have little unique behavior | Transition table | The mapping from current state and input to next state is visible as data rather than distributed among classes. |
| A caller or configuration deliberately selects among interchangeable algorithms | Strategy | Strategy packages algorithms for selection and interchange; State models behavior that follows a machine’s current lifecycle state. |
| Hierarchical states, guards, history, parallel behavior, or a formal model are required | Assess a statechart or FSM library or modeling tool | A basic hierarchy of State classes may not express those features well. Check a candidate’s current capabilities before relying on them. |
These are starting points, not rules based on a particular number of states. Consider how much behavior differs, how many operations branch on state, whether transitions are owned by states or by the machine, whether the graph is regular, and what your language provides for exhaustive enum or sealed-type matching.
When GoF State earns its extra structure
Choose state objects when they clarify meaningful differences in behavior—not merely because the domain has named states. The pattern is most useful when multiple operations vary by state and the logic would otherwise be scattered through conditionals. Grouping behavior can make the lifecycle easier to understand and change.
Rank #2
Godot’s official version 3.3 State-pattern tutorial demonstrates separate Idle and Run state scripts for a game character. It also describes alternatives: child nodes for states, an enum with match logic, or swapping state scripts at runtime. The example illustrates that the pattern is one implementation choice, not a universal prescription; its documentation is specifically for Godot 3.3.
If state classes mostly contain boilerplate, repeat the same logic, or simply rename branches, they may add ceremony without making behavior clearer. Extra types and delegation are worthwhile only when the resulting organization helps people work with the rules.
Rank #3
Who should own transitions?
Decide whether the context or individual state objects trigger transitions, then keep that decision coherent. If the machine owns transitions, state objects can focus on behavior while the context applies transition rules. If states own transitions, each state can express what follows its own events, but the overall graph may be harder to see in one place.
The right split depends on where transition stimuli originate and how uniform the event set is across states. PMI discusses this as a design choice, not a fixed property of the pattern. Whichever approach you use, make valid transitions reviewable and ensure rejected or invalid actions have deliberate outcomes.
State versus Strategy: similar shape, different intent
Both patterns can use a context that delegates work to an interchangeable object, so their class diagrams may look alike. The question is what drives the change. With State, the object’s behavior evolves as its lifecycle state changes. With Strategy, a caller or configuration chooses among algorithms that are intended to be interchangeable.
Choose based on that ownership and intent, not simply on whether both designs expose a similar interface. A payment calculation selected by configuration is an algorithm choice; whether an account can accept a withdrawal because it is open or closed is a lifecycle rule.
Best Value
- Used Book in Good Condition
Costs and testing concerns
- Class proliferation: One class per state can be needless ceremony when the states are few or their behavior barely differs.
- Opaque transitions: Splitting transition decisions inconsistently between the context and state objects makes it harder to understand the machine’s rules.
- Indirection and coupling: Delegation and additional types can obscure a design if the state-specific behavior is trivial or duplicated.
- Testing is not automatically easier: Separate state objects can isolate behavior, but tests still need to cover transitions and invalid actions. Exposing state interfaces for test fakes can also conflict with keeping states encapsulated, a trade-off PMI notes.
There is no general performance advantage established here for GoF State, enums, or tables. If dispatch or allocation costs matter in your application, measure the implementation you actually use rather than assuming one representation is faster.
A practical decision checklist
- List the states, events, valid transitions, and operations whose behavior changes.
- Try the simplest representation that makes those rules clear: a conditional or enum for concise, stable logic; a table for regular data-like transitions.
- Use state objects when substantial behavior differs by state across multiple operations and delegation meaningfully localizes it.
- Choose one clear owner for transitions and define how invalid actions are handled.
- Test state-specific behavior, valid transitions, and rejected transitions; revisit the design if state classes mainly duplicate logic or obscure the graph.
Further reading
For the original catalog treatment and its surrounding patterns, see the publisher listing for Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. InformIT lists the first edition as published by Addison-Wesley Professional on October 31, 1994; it is a 416-page book with ISBN 978-0-201-63361-0.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.

