In the State Pattern, the behavior of a class is changed based on its state. This type of design pattern is a behavioral model.
In state mode, we create objects that represent various states and a context object whose behavior changes as the state object changes.
Intent: Allows an object to change its behavior when the internal state changes, and the object appears to have modified its class.
Main solution: The behavior of an object depends on its state (attribute) and its behavior can be changed based on its state change.
When to use: The code contains a number of conditional statements related to the state of the object.
How to solve: Abstract various concrete state classes.
Key Code: There is usually only one method in the interface of the command mode. There are one or more methods in the state mode interface. Moreover, the state mode implementation class method generally returns a value, or changes the value of an instance variable. That is, the state mode is generally related to the state of the object. The methods that implement classes have different functions, covering methods in the interface. State mode, like command mode, can also be used to eliminate conditional select statements such as if...else.
Application example: 1. When playing basketball, athletes can have normal, abnormal and abnormal conditions. 2, Zeng Houyi bells in the clock, 'clock is the abstract interface', 'clock A' and so on is the specific state, 'Zeng Houyi chime' is the specific environment (Context).
Advantages: 1. Encapsulate the conversion rules. 2. Enumerate the possible states and determine the state type before enumerating the state. 3. Put all the behaviors related to a certain state into a class, and you can easily add new states. You only need to change the state of the objects to change the behavior of the objects. 4. Allow state transition logic to be integrated with state objects, rather than a huge conditional statement block. 5, can allow multiple environment objects to share a state object, thereby reducing the number of objects in the system.
Disadvantages: 1, the use of state mode will inevitably increase the number of system classes and objects. 2, the structure and implementation of the state mode are more complicated, if used improperly will lead to confusion of the program structure and code. 3, the state mode support for the "opening and closing principle" is not very good, for the state mode that can switch state, adding a new state class needs to modify the source code responsible for the state transition, otherwise it can not switch to the new state, and modify The behavior of a state class also needs to modify the source code of the corresponding class.
Usage scenarios: 1. A scenario in which behavior changes as the state changes. 2. Replacement of conditions and branch statements.
Note: Use state mode when the behavior is constrained by state, and the state does not exceed five.
We will create an State interface and an entity state class that implements the State interface. Context is a class with a state.
StatePatternDemo, our demo class uses Context and state objects to demonstrate how the Context changes behavior when the state changes.
Create an interface.
Create an entity class that implements the interface.
Create the Context class.
Use Context to see changes in behavior when the state State changes.
Executing the program, outputting results:
Player is in start state Start State Player is in stop state Stop State