The state pattern is a behavioral pattern that allows an object to change its behavior when its internal state changes. The object appears to have modified its class, and the location of the state mode change lies in the object's state.
Main roles
Abstract state (State) role: Define an interface to encapsulate the behavior corresponding to a specific state of the environment object
Concrete state (ConcreteState) role: Each concrete state class implements the environment ( The behavior corresponding to a state of Context
Environment (Context) role: Define the interface that the client is interested in, and retain an instance of a specific state class. An instance of this concrete state class gives the current state of this environment object
Applicability
The behavior of an object depends on its state, and it must change its behavior based on the state at runtime
There are a huge number of operations in one operation A conditional statement with multiple branches, and these branches depend on the state of the object. This state is usually represented by one or more enumeration constants. Often, there are multiple operations containing this same conditional structure. State mode puts each conditional branch into a separate class. This allows you to treat the state of the object as an object, which can change independently without relying on other objects. It localizes behavior related to a specific state
It makes state transitions explicit
State objects can be shared