java设计模式之状态机模式

①UML设计:

java设计模式之状态机模式_第1张图片

②定义:不同的状态,不同的行为,每个状态有着相应的行为

③示例:

状态机实现电梯运转

*UML设计:

java设计模式之状态机模式_第2张图片

*代码实现:

/**
 * 定义电梯的状态:打开、关闭、运行、停止
 */
interface LiftState {
    void open();

    void close();

    void run();

    void stop();
}
public class OpenState implements LiftState {
    //拥有一个电梯对象,用于更新电梯当前状态
    Lift lift;

    public OpenState(Lift lift) {
        //通过构造函数引入电梯的实例化对象
        this.lift = lift;
    }

    @Override
    public void open() {
        //电梯当前状态--> 打开
        System.out.println("lift is openning");
    }

    @Override
    public void close() {
        System.out.println("lift is closeing");
        //更改电梯当前状态-->关闭
        lift.setState(lift.getCloseingState());
    }

    @Override
    public void run() {
        //电梯处于打开状态,不能直接运行,需要先关闭
        System.out.println("lift is openning can not run should be colsed firstly");
    }

    @Override
    public void stop() {
        //电梯打开的状态是处于停止状态
        System.out.println("lift is in a state of stop");
    }
}
public class CloseState implements LiftState {
    Lift lift;

    public CloseState(Lift lift) {
        this.lift = lift;
    }

    @Override
    public void open() {
        //电梯关闭了再打开,是可以的
        System.out.println("lift is openning");
        //更新电梯当前状态-->打开
        this.lift.setState(lift.getOpenningState());
    }

    @Override
    public void close() {
        //电梯的当前状态-->关闭
        System.out.println("lift is closed");

    }

    @Override
    public void run() {
        //电梯关闭的状态下允许运行
        System.out.println("lift is running");
        //更新电梯当前状态-->运行
        this.lift.setState(lift.getRunningState());
    }

    @Override
    public void stop() {
        //电梯门关着,不按楼层,也可以
        System.out.println("lift is stopping");
        //更新电梯状态-->停止
        this.lift.setState(lift.getStoppingState());
    }
}
public class RunState implements LiftState {
    Lift lift;

    public RunState(Lift lift) {
        this.lift = lift;
    }

    @Override
    public void open() {
        //电梯运行的时候不能开门
        System.out.println("lift is running can not open");
    }

    @Override
    public void close() {
        //电梯运行的时候本来门本来就是关闭的
        System.out.println("lift is closing");
    }

    @Override
    public void run() {
        //电梯当前状态-->运行
        System.out.println(" lift is running");
    }

    @Override
    public void stop() {
        //停止运行电梯
        System.out.println("lift is stopping");
        //更新电梯当前状态-->停止
        this.lift.setState(lift.getStoppingState());
    }
}
public class StopState implements LiftState {
    Lift lift;

    public StopState(Lift lift) {
        this.lift = lift;
    }

    @Override
    public void open() {
        //停止状态可以开门
        System.out.println("lift is openning");
        //更新电梯状态-->打开
        this.lift.setState(lift.getOpenningState());
    }

    @Override
    public void close() {
        //停止状态门本来就是关闭的
        System.out.println("lift is closing");
    }

    @Override
    public void run() {
        //停止状态可以继续运行
        System.out.println("lift is running");
        //更新电梯状态-->运行
        this.lift.setState(lift.getRunningState());
    }

    @Override
    public void stop() {
        //电梯当前状态-->关闭
        System.out.println("lift is stopping");
    }
}
/**
 * 定义电梯context
 */
public class Lift {
    //定义出电梯的所有状态
    private LiftState openningState;
    private LiftState closingState;
    private LiftState runningState;
    private LiftState stoppingState;

    public Lift() {
        openningState = new OpenState(this);
        closingState = new CloseState(this);
        runningState = new RunState(this);
        stoppingState = new StopState(this);

    }

    //定义电梯状态
    LiftState state;

    public void open() {
        state.open();
    }

    public void close() {
        state.close();
    }

    public void run() {
        state.run();
    }

    public void stop() {
        state.stop();
    }

    public void setState(LiftState state) {
        this.state = state;
    }

    public LiftState getOpenningState() {
        return openningState;
    }

    public LiftState getCloseingState() {
        return closingState;
    }

    public LiftState getRunningState() {
        return runningState;
    }

    public LiftState getStoppingState() {
        return stoppingState;
    }

}
class Test3 {
    public static void main(String[] args) {
        Lift lift = new Lift();
        lift.setState(new OpenState(lift));
        lift.open();
        lift.close();
        lift.run();
        lift.stop();
    }
}







你可能感兴趣的:(java设计模式)