The State pattern, also known as Objects for States, is a behavioral GoF design pattern by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides that lets an object alter its behavior when its internal state changes, so that the object appears to change its class. Each state encapsulates its own behavior in a separate object, which replaces large state-dependent conditional statements and offers one way to implement a finite state machine.
GoF-State Pattern
Details
- Full Name
-
GoF State Pattern (Behavioral)
- Also known as
-
Objects for States
Intent:
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
When to Use:
-
When an object’s behavior depends on its state and must change at runtime
-
When operations have large conditional statements that depend on the object’s state
Prompt Example:
"Ersetze die verschachtelten if/switch-Statements durch das GoF-State Pattern, sodass jeder Zustand sein eigenes Verhalten kapselt."
Related Anchors:
-
GoF Design Patterns (Umbrella)
-
Finite State Machine — the model this pattern implements; the pattern is one way to code it