State mode is the behavior of the object in each State and state transfer statements encapsulated in a State class, through these state classes to disperse the lengthy conditional transfer statements.
State pattern: Allows an object to change its behavior when its internal state changes, and the object seems to modify its class. Its alias is a state object (Objects for States), and the state mode is an object-behavioral pattern.
Each subclass implements a behavior related to a state of the environment class, and each specific state class corresponds to a specific state of the environment.
In some cases, multiple environment objects may need to share the same state, and if you want to implement multiple environment objects in your system to share one or more state objects, you need to define these state objects as static member objects of the Environment class . See http://blog.csdn.net/lovelion/article/details/8523105
When implementing state transitions in state mode, the specific state class can transform the state by invoking the SetState () method of the Environment class context, or it can unify the context of the environment to realize the transformation of the state. At this point, adding a new state Class may require modifying the source code of other specific state classes or environment classes, or the system cannot be converted to a new state. However, for the client, there is no need to care about the state class, you can set the default state class for the Environment class, and the transition work of the State to the specific state class or environment class to complete, the specific conversion details for the client is transparent.
The environment class, as a state manager, implements the transformation operation between various states uniformly .
State mode encapsulates the different behavior of an object in different states in a State class, by setting different state objects to allow the environment object to have different behavior, and the details of the state transition is transparent to the client, which facilitates the use of the client. In the actual development, the state mode has a high frequency of use, in the workflow and game development in the state model has been widely used, such as the transformation of the state of the document, the role of the game in the upgrade.
The main advantages of the state mode are as follows:
(1) encapsulates the state of the transformation rules , in the state mode can be the state of the transformation code encapsulated in the Environment class or the specific state class, the state transformation code can be centralized management, rather than scattered in a business method.
(2) putting all behaviors related to a state into a class requires that you inject a different state object to make the environment object behave differently. (I think this is the most useful point.) )
(3) allowing state transition logic to be integrated with State objects rather than providing a huge conditional statement block , state mode allows us to avoid using large conditional statements to weave business methods and state transformation code together.
(4) You can have multiple environment objects share a state object , thereby reducing the number of objects in the system.
From the Liu Wei teacher blog.
State Mode Learning