状态设计模式

package com.jmj.pattern.state.after;

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





}
package com.jmj.pattern.state.after;

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对象
        liftState.setContext(this);
    }
    public  void  open(){
        this.liftState.open();
    }
    public  void  stop(){
        this.liftState.stop();
    }
    public  void  run(){
        this.liftState.run();
    }
    public  void  close(){
        this.liftState.close();
    }


}
package com.jmj.pattern.state.after;

public 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() {
        //什么都不做
    }
}
package com.jmj.pattern.state.after;

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);
        super.context.stop();
    }
}
package com.jmj.pattern.state.after;

public class StoppingState extends LiftState{



    @Override
    public void open() {
        super.context.setLiftState(Context.OPENING_STATE);
        super.context.open();
    }

    @Override
    public void close() {
        super.context.setLiftState(Context.CLOSING_STATE);
        super.context.close();
    }

    @Override
    public void run() {
        super.context.setLiftState(Context.RUNNING_STATE);
        super.context.run();
    }

    @Override
    public void stop() {
        System.out.println("电梯停止...");
    }
}
package com.jmj.pattern.state.after;

public class ClosingState extends LiftState{


    @Override
    public void open() {
        super.context.setLiftState(Context.OPENING_STATE);
        super.context.open();
    }

    @Override
    public void close() {
        System.out.println("电梯关闭...");
    }

    @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();
    }
}
package com.jmj.pattern.state.after;

public class Client {
    public static void main(String[] args) {
        Context context = new Context();
        context.setLiftState(Context.STOPPING_STATE);
        context.run();
     context.stop();
     context.open();
     context.close();
     context.run();
    }
}

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

你可能感兴趣的:(设计模式,设计模式,java,开发语言)