状态模式

栏目: 后端 · 发布时间: 6年前

内容简介:定义电梯接口实现书写场景类

模拟电梯

定义电梯接口

public interface ILift{
    // 开门
    public void open();
    // 关门
    public void close();
    // 能运行
    public void run();
    // 停
    public void stop();
}

实现

public class Lifi implements ILift{
    // 关闭
    public void close(){
        // 关门
    }
    // 开门
    public void open(){
        // 开门
    }
    // 运行
    public void run(){
        // 运行
    }
    // 停止
    public void stop(){
        // 停止
    }
}

书写场景类

public class Client{
    public static void main(String[] args){
        ILift lift = new Lift();
        // 开门
        lift.open();
        // 关门
        lift.close();
        // 运行
        lift.run();
        // 停止
        lift.stop();
    }
}

更改

目前不清楚电梯的状态,所以增加电梯的状态

public interface ILift{
    // 四个状态
    // 开门
    public final static int OPENING_STATE = 1;
    // 关门
    public final static int CLOSEING_STATE = 2;
    // 运行
    public final static int RUNNING_STATE = 3;
    // 停止
    public final static int STOPPING_STATE = 4;
    // 设置状态
    public void setState(int state);
    // 下方相同
}
// 实现类
public class Lift implements ILift{
    private int state;
    public void setState(int state){
        this.state = state;
    }
    // 当电梯门关闭
    public void close(){
        // 关闭
        switch(this.state){
            case OPENING_STATE; // 关门
            this.closeWithoutLogic();
            // 修改状态
        }
    }
    // 下方同理
}

修改

对电梯的状态进行封装,当调用的时候,直接自动改变电梯的状态。

即,迪米特法则

即,外界不需要知道电梯的状态,为一个封装好的。

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();
}

开门状态

public class OpenningState extends LiftState{
    // 关门
    @Override
    public void close(){
        // 状态更改
        super.context.setLiftState(Context.closeingState);
        // 委托执行
        super.context.getLiftState().close();
    }
    // 打开电梯门
    @Override
    public void open(){
        // 开门
    }
    // 运行
    @Override
    public void run(){
        // 门开,禁止运行
    }
    // 关门
    @Override
    public void stop(){
        // 开门的状态即停止
    }
}
// 环境类,用于封装当前电梯的状态
public class Context{
    // 定义电梯状态
    public final static OpenningState openningState = new OpenningState();
    // 剩下三个状态相同
    // 当前状态
    private LiftState liftState;
    // 获得状态
    public LiftState getLiState(){
        return liftState;
    }
    // 设置状态
    public LiftState setLiftState(LiftState liftState){
        this.liftState = liftState;
        // 通知到当前的状态
        this.liftState.setContext(this);
    }
}

剩下的几个相同

// 书写场景类
public class Client{
    public static void main(String[] args){
        // 此时定义一个电梯的状态
        Context context = new Context();
        // 此时电梯进入关闭状态
        // 将关闭状态,保存进入context中
        // 进行通知,将当前状态的父类设置为context
        context.setLiFtState(new ClosingState());
        // 代执行ClosingState
        context.open();
        // 发生关闭状态的时候,
        context.close();
        context.run();
        context.stop();
    }
}

总结

状态模式的核心在于封装,将对象的状态进行封装。

在于通知,当状态发生改变的时候,进行通知。


以上所述就是小编给大家介绍的《状态模式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithms

Data Structures and Algorithms

Alfred V. Aho、Jeffrey D. Ullman、John E. Hopcroft / Addison Wesley / 1983-1-11 / USD 74.20

The authors' treatment of data structures in Data Structures and Algorithms is unified by an informal notion of "abstract data types," allowing readers to compare different implementations of the same......一起来看看 《Data Structures and Algorithms》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具