Java state mode-power node

In state mode (State Pattern), the behavior of the class is based on its state change. This type of design pattern is a behavioral pattern.

In the state mode, we create objects representing various states and a context object whose behavior changes as the state object changes.

Introduction

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

The main solution: the behavior of an object depends on its state (attribute), and its related behavior can be changed according to its state change.

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

How to solve: abstract various concrete state classes.

Key code: Usually there is only one method in the command mode interface. There are one or more methods in the state mode interface. Moreover, the method of the implementation class of the state pattern generally returns a value or changes the value of an instance variable. In other words, the state mode is generally related to the state of the object. The methods of the implementation class have different functions, covering the methods in the interface. The status mode is the same as the command mode, and can also be used to eliminate conditional selection statements such as if...else.

Application examples: ① When playing basketball, players can have normal, abnormal and abnormal states. ② In Zenghouyi chime,'bell is an abstract interface','bell A'is the concrete state, and'Zenghouyi chime' is the context.

Advantages: ① The conversion rules are encapsulated. ② To enumerate the possible states, the type of state needs to be determined before enumerating the state. ③ Put all the behaviors related to a certain state into a class, and you can easily add new states, and you only need to change the state of the object to change the behavior of the object. ④ The state transition logic is allowed to be integrated with the state object instead of a huge conditional statement block. ⑤ You can let multiple environment objects share a state object, thereby reducing the number of objects in the system.

Disadvantages: ① The use of state mode will inevitably increase the number of system classes and objects. ② The structure and implementation of the state mode are relatively complicated, and if used improperly, it will cause confusion in the program structure and code. ③ The state mode does not support the "opening and closing principle" very well. For the state mode that can switch the state, adding a new state class needs to modify the source code responsible for the state transition, otherwise it will not be able to switch to the new state and modify a certain state. The behavior of a state class also needs to modify the source code of the corresponding class.

Use scenario: ① A scenario where the behavior changes with the status change. ② Replacement of conditions and branch statements.

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

achieve

We will create a State interface and an entity state class that implements the State interface. Context is a class with a certain state.

StatePatternDemo, our demo class uses Context and state objects to demonstrate the behavior changes of Context when the state changes.

step 1

Create an interface.

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

Step 2

Create an entity class that implements the interface.

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”;
}
}
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 the Context class.

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 view behavior changes when the state changes.

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

Execute the program and output the result:

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

Guess you like

Origin blog.csdn.net/weixin_49543720/article/details/112574201