编程语言
首页 > 编程语言> > 基于JAVA的设计模式之状态模式

基于JAVA的设计模式之状态模式

作者:互联网

    通过改变对象的属性状态间接改变对象的行为表现。比如电梯处于运行状态就不能开门,当电梯处于关闭状态时就可以开门了。原理就是父类引用指向派生类对象,派生类为状态的具体实现,父类为状态的接口,派生类将父类的接口重写成不同的具体行为,其实就是多态。各个实现状态为同一个父接口且互相依赖、通信。

  

 

/**
 * 状态-行为接口
 */
public interface State {
    //开门
    public void open();
    //关门
    public void close();
    //运行
    public void run();
    //停止
    public void stop();
}

public class OpenState implements State {
    Lift lift;
    public OpenState(Lift lift){
        this.lift=lift;
    }
    public void open() {
        System.out.println("电梯已经处于开门状态");
    }

    public void close() {
        //更改到关门状态
        System.out.println("电梯正在关闭");
        lift.setState(lift.getCLOSE_STATE());
        System.out.println("电梯已经关闭");
    }

    public void run() {
        System.out.println("电梯处于开门状态,请先关门,再运行");
    }

    public void stop() {
        System.out.println("电梯处于关门状态,目前就是停止状态");
    }
}

public class CloseState implements State {
    Lift lift;
    public CloseState(Lift lift){
        this.lift=lift;
    }
    public void open() {
        System.out.println("电梯正在开门");
        lift.setState(lift.getOPEN_STATE());
        System.out.println("电梯已经开门");
    }

    public void close() {
        System.out.println("电梯已经处于关门状态");
    }

    public void run() {
        System.out.println("电梯开始运行");
        lift.setState(lift.getRUN_STATE());
    }

    public void stop() {
        System.out.println("电梯已被停止");
        lift.setState(lift.getSTOP_STATE());
    }
}

public class RunState implements State {
    Lift lift;
    public RunState(Lift lift){
        this.lift=lift;
    }
    public void open() {
        System.out.println("电梯处于运行状态,不能开门");
    }

    public void close() {
        System.out.println("电梯处于运行状态,门本就是关闭状态");
    }

    public void run() {
        System.out.println("电梯已经处于运行状态");
    }

    public void stop() {
        System.out.println("电梯即将停止");
        lift.setState(lift.getSTOP_STATE());
        System.out.println("电梯已经停止");
    }
}

public class StopState implements State {
    Lift lift;
    public StopState(Lift lift){
        this.lift=lift;
    }
    public void open() {
        System.out.println("电梯正在开门");
        lift.setState(lift.getOPEN_STATE());
        System.out.println("电梯已经开门");
    }

    public void close() {
        System.out.println("目前处于停止状态,门本就是关闭状态");
    }

    public void run() {
        System.out.println("电梯即将运行");
        lift.setState(lift.getRUN_STATE());
        System.out.println("电梯处于运行状态");
    }

    public void stop() {
        System.out.println("电梯已经处于停止状态");
    }
}

public class Lift implements State{
    private State OPEN_STATE;
    private State CLOSE_STATE;
    private State RUN_STATE;
    private State STOP_STATE;
    public Lift(){
        OPEN_STATE=new OpenState(this);
        CLOSE_STATE=new CloseState(this);
        RUN_STATE=new RunState(this);
        STOP_STATE=new StopState(this);
    }
    //当前电梯状态
    State state;

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

    public State getOPEN_STATE() {
        return OPEN_STATE;
    }

    public State getCLOSE_STATE() {
        return CLOSE_STATE;
    }

    public State getRUN_STATE() {
        return RUN_STATE;
    }

    public State getSTOP_STATE() {
        return STOP_STATE;
    }

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

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

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

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

public class Main {
    public static void main(String[] args) {
        Lift lift=new Lift();
        lift.setState(lift.getOPEN_STATE());
        lift.close();
        lift.run();
    }
}

 

标签:基于,JAVA,void,System,STATE,lift,println,设计模式,public
来源: https://www.cnblogs.com/hbsdljz/p/11090158.html