Design Patterns (GoF) (50 Qs)•Part 2: Behavioral Patterns & Communication (11 - 24)
What is the State Pattern and how does it differ from the Strategy Pattern?
junior LevelBehavioral
In Simple Words: Allows an object to alter its behavior when its internal state changes, appearing as if the object changed its class:
- Strategy vs State:
- Strategy: Client explicitly chooses which algorithm strategy to inject; strategies rarely know about other strategies.
- State: The context's behavior changes automatically as internal states transition between themselves (e.g.
DraftState->ReviewState->PublishedState).