Small Talk on Design Patterns (17)—State Pattern

Column introduction

Column address

link

Column introduction

It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
Insert image description here

state mode

The State pattern is a behavioral design pattern that allows an object to change its behavior when its internal state changes. The state pattern encapsulates the behavior of an object in different state classes, and changes its behavior by changing the state of the object.
Insert image description here

key role

Context

Context is an object containing state, which defines the interaction interface between the client and the state object. The context maintains a reference to the current state and can switch to a different state at runtime. The context delegates client requests to the current state object for processing.

Abstract state(State)

Abstract state is an interface or abstract class that defines the common behavior of state objects. The concrete state class needs to implement this interface or inherit this abstract class, and implement corresponding behavior according to the specific state.

Concrete State

Concrete state is a concrete class that implements abstract state. Each concrete state class represents the behavior of the context in a specific state. The concrete state class is responsible for handling the context's requests and switching to other states when needed.
Insert image description here

main idea

Separate state judgment and state behavior so that changes in state do not affect changes in behavior. By encapsulating the behavior of states in specific state classes, you can easily add new states or modify the behavior of existing states, while also avoiding the complexity of state judgment.
Insert image description here

Java program implementation

First, we define an abstract state class State, which contains a method handleRequest() for processing requests:

public abstract class State {
    
    
    public abstract void handleRequest();
}

Then, we create two concrete state classes, ConcreteStateA and ConcreteStateB, which implement the abstract state class State respectively:

public class ConcreteStateA extends State {
    
    
    @Override
    public void handleRequest() {
    
    
        System.out.println("处理请求,当前状态为A");
    }
}

public class ConcreteStateB extends State {
    
    
    @Override
    public void handleRequest() {
    
    
        System.out.println("处理请求,当前状态为B");
    }
}

Next, we create a context class Context, which contains a reference to the current state, and provides a method setState() for switching states and a method request() for processing requests:

public class Context {
    
    
    private State currentState;

    public Context() {
    
    
        // 初始化为初始状态
        currentState = new ConcreteStateA();
    }

    public void setState(State state) {
    
    
        currentState = state;
    }

    public void request() {
    
    
        currentState.handleRequest();
    }
}

Finally, we can use context classes in client code to test the effects of the stateful pattern:

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

        // 处理请求,当前状态为A
        context.request();

        // 切换状态为B
        context.setState(new ConcreteStateB());

        // 处理请求,当前状态为B
        context.request();
    }
}

Output results

处理请求,当前状态为A
处理请求,当前状态为B

analyze

In the above example, we implemented a simple context object Context through the state pattern, which can handle requests according to different states. By switching states, a context object can change its behavior. In this way, we can easily add new state classes or modify the behavior of existing states without modifying the client code.
Insert image description here

Advantages and Disadvantages Analysis

advantage

1

By encapsulating state behavior in a specific state class, state changes can be made transparent to the client. The client only needs to interact with the context and does not need to care about the specific state.

2

Adding new status classes is relatively easy, conforms to the opening and closing principle, and does not require modification of existing code.

3

Concentrating state behavior into specific state classes makes the code clearer and easier to maintain and expand.

shortcoming

1

When the behavior of the state is relatively small or simple, using the state pattern may lead to an increase in the number of classes and increase the complexity of the code.

2

If there is complex logic for converting between states, other patterns may need to be introduced to handle the transitions between states.
Insert image description here

Summarize

The state pattern is a design pattern that encapsulates the behavior of the state in a specific state class so that changes in the state do not affect the behavior. It can make the code clearer, easier to maintain and expand, and is suitable for scenarios where there are many state changes and large differences in behavior between states.

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/133561103