[Design Mode] - 18, Status Mode

(18) Status mode

The State Pattern allows an object to change its state when its internal state changes, and the behavior of a class changes based on its state.

1. The design principles of the state pattern

1. Context: defines the interface required by the client, maintains a current state instance internally, and is responsible for state switching;

2. Abstract state (IState): defines the behavior of the state;

3. Concreate: The behavior of a concrete state.

image-20210318142941479

2. Simple example

When we read articles in the community, operations such as comments and collections need to be performed in the logged-in state, otherwise, we will jump to the login page uniformly. Next, we will use the state pattern to realize the above scenario.

public abstract class UserState {
    
    
//    状态接口需要维护一个context
    protected AppContext appContext;

    public void setAppContext(AppContext appContext) {
    
    
        this.appContext = appContext;
    }

    public abstract void favorite();
    public abstract void comment(String comment);
}
public class LoginState extends UserState {
    
    
    @Override
    public void favorite() {
    
    
        System.out.println("收藏成功!");
    }

    @Override
    public void comment(String comment) {
    
    
        System.out.println(comment);
    }
}
public class UnLoginState extends UserState {
    
    
//    未登录状态下的行为多了一个切换操作
    @Override
    public void favorite() {
    
    
        this.switch2Login();
        this.appContext.getState().favorite();
    }

    @Override
    public void comment(String comment) {
    
    
        this.switch2Login();
        this.appContext.getState().favorite();
    }
//    切换到登录
    public void switch2Login(){
    
    
        System.out.println("跳转到登录页面!");
        this.appContext.setState(this.appContext.STATE_LOGIN);
    }
}
public class AppContext {
    
    
//    context内部维护两种状态
    public static final UserState STATE_LOGIN = new LoginState();
    public static final UserState STATE_UNLOGIN = new UnLoginState();

    private UserState currentState = STATE_UNLOGIN;

//    代码块初始化状态对应的context
    {
    
    
        STATE_LOGIN.setAppContext(this);
        STATE_UNLOGIN.setAppContext(this);
    }
//   设置状态,同时设置当前状态的context
    public void setState(UserState state){
    
    
        this.currentState = state;
        this.currentState.setAppContext(this);
    }
    public UserState getState(){
    
    
        return this.currentState;
    }
//    内部需要维护当前状态的方法
    public void favorite(){
    
    
        this.currentState.favorite();
    }

    public void comment(String comment){
    
    
        this.currentState.comment(comment);
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        AppContext appContext = new AppContext();
        appContext.favorite();
        appContext.comment("我赞!");
    }
}
跳转到登录页面!
收藏成功!
我赞!

image-20210318145954302

3. Comments on Status Mode

The state mode switches the state through the context, which well encapsulates the transition details. But he violates the open-closed principle and must know all states.

Compared with the Chain of Responsibility model, the State Model state knows the next state, but the Chain of Responsibility model does not know, it is encapsulated in the client;

Compared with the strategy mode, the state mode is related to each other, and the strategies are independent of each other.

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118343231