Design Patterns State Pattern Notes

illustrate

Record the writing method of learning design pattern-state pattern. The JDK version used is version 1.8.

State(state)

Intent : Allow an object to change its behavior when its internal state changes.
Structure :
Insert image description here

in:

  • Context defines the interface that the client is interested in; maintains an instance of the ConcreteState subclass, which defines the current state.
  • State defines an interface to encapsulate behavior related to a specific state of the Context.
  • ConcreteState (concrete state subclass) Each subclass implements behavior related to a state of Context.

applicability:

  • An object's behavior is determined by its state, and it must change its behavior based on the state at run time.
  • An operation contains a large number of conditional statements with multiple branches, and these branches depend on the state of the object.

Table of contents

Insert image description here

State Pattern Example Class Diagram

[Example] Use buttons to control the status of an elevator. An elevator has a door-opening state, a closing state, a stopped state, and a running state. Every state change may need to be updated based on other states. For example, if the elevator door is currently in a running state, the door opening operation cannot be performed, but if the elevator door is in a stopped state, the door opening operation can be performed.
Insert image description here

Implement the state pattern example with this UML class diagram.

abstract state class

package com.example.deesign_patterns.state;

//抽象状态类
public abstract class LiftState {
    
    

    //定义一个环境角色,也就是封装状态的变化引起的功能变化
    //声明环境角色类变量
    protected Context context;

    public void setContext(Context context) {
    
    
        this.context = context;
    }

    //电梯开启操作
    public abstract void open();

    //电梯关闭操作
    public abstract void close();

    //电梯运行操作
    public abstract void run();

    //电梯停止操作
    public abstract void stop();
}

Environmental role class

package com.example.deesign_patterns.state;

//环境角色类
public class Context {
    
    

    //定义对应状态对象的常量
    public final static OpeningState OPENING_STATE=new OpeningState();
    public final static ClosingState CLOSING_STATE=new ClosingState();
    public final static RunningState RUNNING_STATE=new RunningState();
    public final static StoppingState STOPPING_STATE=new StoppingState();

    //定义一个当前电梯状态变量
    private LiftState liftState;

    public LiftState getLiftState() {
    
    
        return liftState;
    }

    //设置当前状态对象
    public void setLiftState(LiftState liftState) {
    
    
        this.liftState = liftState;
        //设置当前状态对象中的Context对象
        this.liftState.setContext(this);
    }

    public void open(){
    
    
        this.liftState.open();
    }

    public void close(){
    
    
        this.liftState.close();
    }

    public void run(){
    
    
        this.liftState.run();
    }

    public void stop(){
    
    
        this.liftState.stop();
    }
}

Elevator open status class

package com.example.deesign_patterns.state;

//电梯开启状态类
public class OpeningState extends LiftState{
    
    

    //当前状态要执行的方法
    @Override
    public void open() {
    
    
        System.out.println("电梯开启。。。");
    }

    @Override
    public void close() {
    
    
        //修改状态
        super.context.setLiftState(Context.CLOSING_STATE);
        //调用当前状态中的context中的close方法
        super.context.close();
    }

    @Override
    public void run() {
    
    
        //因为要开启,还没到运行所以什么都不做
    }

    @Override
    public void stop() {
    
    
        //因为要开启,还没到停止所以什么都不做
    }
}

Elevator closed status class

package com.example.deesign_patterns.state;

//电梯关闭状态类
public class ClosingState extends LiftState{
    
    

    //电梯门关闭再打开,很合理
    @Override
    public void open() {
    
    
        //修改状态
        super.context.setLiftState(Context.OPENING_STATE);
        //调用当前状态中的context中的open方法
        super.context.open();
    }

    //当前状态要执行的方法
    @Override
    public void close() {
    
    
        System.out.println("电梯门关闭。。。");
    }

    //电梯门关了再运行,很合理
    @Override
    public void run() {
    
    
        //修改状态
        super.context.setLiftState(Context.RUNNING_STATE);
        //调用当前状态中的context中的run方法
        super.context.run();
    }

    //电梯门关着,我就不按楼层
    @Override
    public void stop() {
    
    
        //修改状态
        super.context.setLiftState(Context.STOPPING_STATE);
        //调用当前状态中的context中的stop方法
        super.context.stop();
    }
}

Elevator running status class

package com.example.deesign_patterns.state;

//电梯运行状态类
public class RunningState extends LiftState{
    
    

    //运行的时候开电梯门,是不允许的,所以什么都不做
    @Override
    public void open() {
    
    

    }

    //运行状态电梯门肯定是关闭的,所以什么也不做
    @Override
    public void close() {
    
    

    }

    //当前状态要执行的方法
    @Override
    public void run() {
    
    
        System.out.println("电梯正在运行。。。");
    }

    //既能运行,必然要停止
    @Override
    public void stop() {
    
    
        //修改状态
        super.context.setLiftState(Context.STOPPING_STATE);
        //调用当前状态中的context中的stop方法
        super.context.stop();
    }
}

Elevator stop status class

package com.example.deesign_patterns.state;

//电梯停止状态类
public class StoppingState extends LiftState{
    
    

    //电梯停止再开门,很合理
    @Override
    public void open() {
    
    
        //修改状态
        super.context.setLiftState(Context.OPENING_STATE);
        //动作委托为CloseState来执行,也就是委托给ClosingState子类执行这个歌动作
        super.context.getLiftState().open();
    }

    //虽然可以关闭,但这个状态不归我管
    @Override
    public void close() {
    
    
        //修改状态
        super.context.setLiftState(Context.CLOSING_STATE);
        //动作委托为CloseState来执行,也就是委托给ClosingState子类执行这个歌动作
        super.context.getLiftState().close();
    }

    //电梯停止再运行起来,很合理
    @Override
    public void run() {
    
    
        //修改状态
        super.context.setLiftState(Context.RUNNING_STATE);
        //动作委托为CloseState来执行,也就是委托给ClosingState子类执行这个歌动作
        super.context.getLiftState().run();
    }

    //当前状态要执行的方法
    @Override
    public void stop() {
    
    
        System.out.println("电梯停止了。。。");
    }
}

Test class

package com.example.deesign_patterns.state;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //创建环境角色对象
        Context context=new Context();
        //设置当前电梯状态,这里设置为正在运行状态
        context.setLiftState(new RunningState());
        context.open();
        context.close();
        context.run();
        context.stop();
    }
}


Insert image description here

benefit:

  • Put all the behaviors related to a certain state into a class, and you can easily add new states. You only need to change the object state to change the object's behavior.
  • Allows state transition logic to be integrated with the state object instead of a huge conditional statement block.

shortcoming:

  • The use of state mode will inevitably increase the number of system classes and objects.
  • The structure and implementation of the state pattern are relatively complex. If used improperly, it will lead to confusion in the program structure and code.
  • The state pattern does not support the open-closed principle very well.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131361698