This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
What is a state mode?
When an object’s internal state changes, allowing it to change its behavior, the object appears to change its class.
The behavior of the objects he studies is wrapped in different state objects, each of which belongs to a subclass of an abstract state class. The purpose of a state pattern is to make an object’s behavior change as its internal state changes.
The main problem he addresses is that an object’s behavior depends on its state (properties) and can change its related behavior as its state changes.
Code sample
Xiao Ming is a professional basketball player, and when he sweates a lot on the basketball court, there may be normal state, abnormal state and abnormal state.
First define a state superclass interface:
public interface State {
void doAction(Context context);
}
Copy the code
Then define a context class for switching states:
public class Context {
private State state;
public Context(a){
state = null;
}
public void setState(State state){
this.state = state;
}
public void doAction(a) {
this.state.doAction(this);// Handle the request and point to the next state}}Copy the code
Then define different state classes:
Supernormal state class:
The abnormal state may be switched to the normal state.
public class SupernormalState implements State {
public void doAction(Context context) {
System.out.println("Xiao Ming is in a supernormal state.");
Random random = new Random();
if(random.nextInt(10) %2= =0){
System.out.println("State switch: Xiao Ming switches from abnormal to normal");
context.setState(newNormalState()); }}public String toString(a){
return "Supernormal state"; }}Copy the code
Normal status class:
It is possible to switch from the normal state to the abnormal state and abnormal state.
public class NormalState implements State {
public void doAction(Context context) {
System.out.println("Xiao Ming is in a normal state.");
Random random = new Random();
if(random.nextInt(10) %3= =0){
System.out.println("State switch: Xiao Ming switches from normal state to abnormal state");
context.setState(new SupernormalState());
} else if(random.nextInt(10) %3= =1){
System.out.println("State switch: Xiao Ming switches from normal state to abnormal state");
context.setState(newAbnormalState()); }}public String toString(a){
return "Normal state"; }}Copy the code
Abnormal state class:
The abnormal state may be switched to the normal state.
public class AbnormalState implements State {
@Override
public String toString(a) {
return "Abnormal state";
}
@Override
public void doAction(Context context) {
System.out.println("Xiao Ming is in an abnormal state.");
Random random = new Random();
if(random.nextInt(10) %2= =0){
System.out.println("State switch: Xiao Ming switches from abnormal to normal state");
context.setState(newNormalState()); }}}Copy the code
The test class:
public class StatePatternDemo { public static void main(String[] args) { Context context = new Context(); context.setState(new NormalState()); System.out.println(" Start of the first session "); context.doAction(); System.out.println(" Start of the second session "); context.doAction(); System.out.println(" Start of third quarter "); context.doAction(); System.out.println(" Fourth quarter start "); context.doAction(); }}Copy the code
Running result:
The first quarter begins
Xiao Ming is in a normal state
The second quarter begins
Xiao Ming is in a normal state
State switch: Xiao Ming switches from normal state to abnormal state
The third quarter begins
Xiao Ming is out of order
State switch: Xiao Ming switches from abnormal to normal state
The fourth quarter began
Xiao Ming is in a normal state
State switch: Xiao Ming switches from normal state to abnormal state
conclusion
Usage scenario:
- A scenario in which behavior changes as states change.
- A substitute for a condition or a branch statement.
How to solve:
- Abstract the various concrete state classes.
Advantages:
- Encapsulates the transformation rules.
- Enumerates possible states. Before enumerating states, you need to determine the type of state.
- Put all the behavior associated with a state into one class, and you can easily add new states, changing the object’s behavior simply by changing the object’s state.
- Allows the state transition logic to be integrated with the state object rather than a single giant conditional block.
- You can share a state object with multiple environment objects to reduce the number of objects in the system.
Disadvantages:
- The use of state mode necessarily increases the number of system classes and objects.
- The structure and implementation of state pattern are complicated, and improper use will lead to confusion of program structure and code.
- State modes do not support the “open closed principle” very well. For state modes that can switch states, adding new state classes requires changing the source code responsible for the transition to the new state, and changing the behavior of a state class requires changing the source code of the corresponding class.