You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The state pattern is a behavioral design pattern in which an object can alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines.
The state pattern is used in computer programming to encapsulate varying behavior for the same object based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to conditional statements and thus improve maintainability.
Structure
It consists of context and state.
Context: A context class that will contain the state object.
State: A state class that will contain the action to be performed.
It localizes state-specific behavior and partitions behavior for different states.
It makes state transitions explicit.
State objects can be shared.
Similarity with other patterns
The state pattern can be interpreted as a strategy pattern, which is able to switch a strategy through invocations of methods defined in the pattern's interface.