Glossary · Automation software engineering and architecture
State pattern
Also known as: State design pattern
German: Zustandsmuster
In software design, the state pattern is a design pattern in which an object delegates its state-dependent behavior to separate state objects, changing its behavior by switching the current state object.
- Software engineering
In one sentence
The state pattern delegates state-dependent behavior to separate state objects and changes behavior by switching them.
Example
A machine module implements each PackML-style state such as Idle, Execute and Stopped as a separate class with its own reactions to commands.
How it applies
- Engineering: The pattern replaces large case statements with one unit per state, which makes adding states easier. In PLC code, a CASE-based state machine is more common and often easier for maintenance staff to read.
- Verification: Whatever the implementation, the state model should be specified first; tests then cover each state and transition.
- Documentation: The state model, not the code pattern, belongs in user documentation: states, transitions, commands and indications. A diagram from the specification serves developers and operators alike.
State pattern vs. strategy pattern
Both delegate behavior to exchangeable objects. In the state pattern, the object switches states itself as a result of events. In the Strategy pattern, a client chooses the algorithm and it does not change on its own.