Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

State mode


May 27, 2021 Design mode


Table of contents


State mode

In State Pattern, a class's behavior changes based on its state. This type of design pattern is behavioral.

In state mode, we create objects that represent various states and a context object that changes behavior as state objects change.

Introduced

Intent: Allows an object to change its behavior when its internal state changes, and the object appears to modify its class.

Main solution: An object's behavior depends on its state (attribute) and can change its related behavior depending on its state.

When to use: The code contains a large number of conditional statements related to the state of the object.

How to solve: Abstract out various specific state classes.

Key code: There is usually only one method in the interface of the command mode. T here are one or more methods in the interface of the state pattern. A lso, the method of implementing the class of state patterns typically returns a value, or changes the value of an instance variable. T hat is, state patterns are generally related to the state of an object. T he method of implementing the class has different functions, covering the methods in the interface. S tate mode, like command mode, can also be used to eliminate if... Select statements such as else conditions.

Application example: 1, when playing basketball, athletes can have normal state, abnormal state and abnormal state. 2, Zeng Hou B clock, 'clock is abstract interface', 'clock A' and so on is the specific state, 'Zeng Hou B clock' is the specific environment (Context).

Pros: 1, encapsulating conversion rules. 2 , enumeration of possible states, before enumeration of the state needs to determine the type of state. 3 , put all the behavior related to a state into a class, and can easily add a new state, only need to change the state of the object to change the behavior of the object. 4 . Allow state transition logic to be synthesized with state objects, rather than a huge block of conditional statements. 5 , can let multiple environment objects share a state object, thereby reducing the number of objects in the system.

Cons: 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 complex, if not used properly will lead to the structure of the program and code confusion. 3 , state mode on the "open and close principle" support is not very good, for the state mode can switch states, adding new state classes need to modify those responsible for state transition source code, otherwise can not switch to the new state, and modify the behavior of a state class also need to modify the corresponding class source code.

Use scenes: 1, scenes where behavior changes with the state. 2, the condition, the replacement of the branch statement.

Note: State mode is used when behavior is constrained by state, and there are no more than 5 states.

Realize

We'll create a State interface and implement the State interface's entity state class. Context is a class with a state.

StaePatternDemo, our demo class uses Context and state objects to demonstrate changes in Context's behavior as the state changes.

State mode

Step 1

Create an interface.

State.java

public interface State {
   public void doAction(Context context);
}

Step 2

Create an entity class that implements the interface.

StartState.java

public class StartState implements State {

   public void doAction(Context context) {
      System.out.println("Player is in start state");
      context.setState(this); 
   }

   public String toString(){
      return "Start State";
   }
}

StopState.java

public class StopState implements State {

   public void doAction(Context context) {
      System.out.println("Player is in stop state");
      context.setState(this); 
   }

   public String toString(){
      return "Stop State";
   }
}

Step 3

Create a Context class.

Context.java

public class Context {
   private State state;

   public Context(){
      state = null;
   }

   public void setState(State state){
      this.state = state;      
   }

   public State getState(){
      return state;
   }
}

Step 4

Use Context to see how behavior changes when the state State changes.

StatePatternDemo.java

public class StatePatternDemo {
   public static void main(String[] args) {
      Context context = new Context();

      StartState startState = new StartState();
      startState.doAction(context);

      System.out.println(context.getState().toString());

      StopState stopState = new StopState();
      stopState.doAction(context);

      System.out.println(context.getState().toString());
   }
}

Step 5

Verify the output.

Player is in start state
Start State
Player is in stop state
Stop State