state mode

Status mode:

    When an object's behavior depends on its state, and an input event or over time changes the object's state. Then encapsulate behavior into states and design a class for each state.

   example:

        Control the protagonist's behavior based on the player's input -- "Game Programming Mode"

        The realization of the candy machine, the behavior of the candy machine is controlled according to the customer's operation--《HeadFirst Design Pattern》

        can draw a state transition diagram


State pattern class diagram:


The state transition between concreteStateA and ConcreteStateB is transparent to Context, Context only knows the initial state and current state, but does not know how to convert the state.


State Pattern Design Process

        1. Define the state interface, and each state-related behavior is defined as a virtual function. 【State.

        2. Define a class for each specific state that inherits from the state interface and implements specific behaviors. 【ConCreteState

        3. Delegate the behavior of the object to the behavior of the current state. [Context's request is delegated to state's handle()

                The player's handleInput calls curState's handleInput

                The InsertQuarter of the candy machine calls the InsertQuarter of curState


How state objects are generated

1. Static state

        Restriction: A state cannot have its own data members.

        Advantage: Multiple context objects can share state.

                   In state.handle, you need to pass context as a parameter to state.handle(context*)

2. Instantiate state

        1) Instantiated by context, owns all state instances. The current state is one of these instances. (Each state can have a context pointer data member, and there is no need to pass in the context pointer when handling)

        2) The new state is dynamically created by the concrete state class, and the old state is destroyed by the context.



Disadvantages of State Patterns

1) The use of the state pattern will inevitably increase the number of system classes and objects.

2) The structure and implementation of the state mode are complex, and if used improperly, it will lead to confusion in the program structure and code


First judge the input, then judge the current state, and act according to the state.

vs

First determine the current state, then determine what the input is, and then act according to the input.

vs

Design the state as a class, and design the behavior according to the input into the function of the class.


Compare with strategy pattern

Same as: class diagram. Both the primary object's behavior is delegated to the subordinate objects

Different: The goal is different:

          The goal of the strategy pattern is to decouple the main class from some of its behavior. In the strategy mode, the [strategy object] to be combined with the context is actively formulated.

          The goal of the state pattern is to change the behavior of the main object by changing the object that the main object proxies. In the state mode, the context does not know much about the changes of the [state object], but the state transition is controlled by the [state object].  


Strategy Pattern - A flexible alternative to inheritance.

State Patterns — An alternative to preventing many conditions in the context.


References

"headfirst design pattern"

"Game Programming Mode"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324515562&siteId=291194637