Featured image of post Java设计模式-状态模式State

Java设计模式-状态模式State

相关文章

【合集】Java设计模式

状态模式

环境角色类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Getter
public static 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 void setLiftState(LiftState liftState) {
        this.liftState = liftState;
        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();
    }
}

抽象状态类

1
2
3
4
5
6
7
8
@Setter
public static abstract class LiftState {
    protected Context context;
    public abstract void open();
    public abstract void close();
    public abstract void run();
    public abstract void stop();
}

具体状态类

OpeningState

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public static class OpeningState extends LiftState {
    @Override
    public void open() {
        System.out.println("电梯开启。。。");
    }
    @Override
    public void close() {
        super.context.setLiftState(Context.CLOSING_STATE);
        super.context.close();
    }
    @Override
    public void run() {
    }
    @Override
    public void stop() {
    }
}

ClosingState

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public static class ClosingState extends LiftState {
    @Override
    public void close() {
        System.out.println("电梯门关闭...");
    }
    @Override
    public void open() {
        super.context.setLiftState(Context.OPENING_STATE);
        super.context.open();
    }
    @Override
    public void run() {
        super.context.setLiftState(Context.RUNNING_STATE);
        super.context.run();
    }
    @Override
    public void stop() {
        super.context.setLiftState(Context.STOPPING_STATE);
        super.context.stop();
    }
}

RunningState

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static class RunningState extends LiftState {
    public void run() {
        System.out.println("电梯正在运行...");
    }
    @Override
    public void open() {
    }
    @Override
    public void close() {
    }
    @Override
    public void stop() {
        super.context.setLiftState(Context.STOPPING_STATE);
        super.context.stop();
    }
}

StoppingState

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public static class StoppingState extends LiftState {
    public void stop() {
        System.out.println("电梯停止了...");
    }
    public void open() {
        super.context.setLiftState(Context.OPENING_STATE);
        super.context.getLiftState().open();
    }
    public void close() {
        super.context.setLiftState(Context.CLOSING_STATE);
        super.context.getLiftState().close();
    }
    public void run() {
        super.context.setLiftState(Context.RUNNING_STATE);
        super.context.getLiftState().run();
    }
}

客户端调用

1
2
3
4
5
6
7
Context context = new Context();
context.setLiftState(new ClosingState());

context.open();
context.run();
context.close();
context.stop();

输出

1
2
3
电梯开启。。。
电梯门关闭...
电梯停止了...
皖ICP备2024056275号-1
发表了78篇文章 · 总计149.56k字
本站已稳定运行