[6] [状态变化] ( 2 ) 状态模式 State

总结

  • GOF定义?
    允许一个对象, 在内部状态改变时, 改变它的行为, 从而使对象看起来似乎修改了其行为.


  • 优点?
    状态转换的逻辑被分离, 状态行为被分离, 每一个状态都是一个单独的类,
    更容易添加新的状态, 或者修改状态转换的逻辑.
    .
    同时消除了ifelse,
    状态转换的逻辑更加清晰,
    更加易于理解.


  • 场景案例?
    游戏角色状态切换
    网络连接状态
    订单状态


  • 和策略模式相似
    该模式提供了if else以外的另一种选择,
    消除条件判断语句,
    就是在解耦合.




java例子1

重构前

package v29_state.java;

class Character {
    private String state = "idle";

    public void walk() {
        if (state.equals("idle")) {
            state = "walking";
            System.out.println("The character is now walking.");
        } else {
            System.out.println("The character cannot walk right now.");
        }
    }

    public void run() {
        if (state.equals("walking")) {
            state = "running";
            System.out.println("The character is now running.");
        } else {
            System.out.println("The character cannot run right now.");
        }
    }

    public void jump() {
        if (state.equals("running")) {
            state = "jumping";
            System.out.println("The character is now jumping.");
        } else {
            System.out.println("The character cannot jump right now.");
        }
    }

    public void idle() {
        state = "idle";
        System.out.println("The character is now idle.");
    }
}

public class Game1 {
    public static void main(String[] args) {
        Character character = new Character();

        character.walk(); // The character is now walking.
        character.run(); // The character is now running.
        character.jump(); // The character is now jumping.
        character.idle(); // The character is now idle.

        character.run(); // The character cannot run right now.
        character.jump(); // The character cannot jump right now.
    }
}





重构后

package v29_state.java;

interface State {
    void walk();
    void run();
    void jump();
    void idle();
}

class IdleState implements State {}

class WalkingState implements State {}

class RunningState implements State {}

class JumpingState implements State {
    private Character character;

    public JumpingState(Character character) {
        this.character = character;
    }

    public void walk() {
        System.out.println("The character cannot walk right now.");
    }

    public void run() {
        System.out.println("The character cannot run right now.");
    }

    public void jump() {
        System.out.println("The character is already jumping.");
    }

    public void idle() {
        character.setState(character.getIdleState());
        System.out.println("The character is now idle.");
    }
}

class Character {
    private State idleState;
    private State walkingState;
    private State runningState;
    private State jumpingState;

    private State state;

    public Character() {
        idleState = new IdleState(this);
        walkingState = new WalkingState(this);
        runningState = new RunningState(this);
        jumpingState = new JumpingState(this);

        state = idleState;
    }

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

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

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

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

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

    // Getter methods for the states
    public State getIdleState() {
        return idleState;
    }

    public State getWalkingState() {
        return walkingState;
    }

    public State getRunningState() {
        return runningState;
    }

    public State getJumpingState() {
        return jumpingState;
    }
}


public class Game2 {
    public static void main(String[] args) {
        Character character = new Character();

        character.walk(); // The character is now walking.
        character.run(); // The character is now running.
        character.jump(); // The character is now jumping.
        character.idle(); // The character is now idle.

        character.run(); // The character cannot run right now.
        character.jump(); // The character cannot jump right now.
    }
}





image




image




java例子2

package v29_state.java;


interface TrafficLightState {
    void handle(TrafficLight trafficLight);
}

class RedState implements TrafficLightState {
    @Override
    public void handle(TrafficLight trafficLight) {
        System.out.println("Red Light: Stopped");
        trafficLight.setState(new GreenState());
    }
}

class YellowState implements TrafficLightState {
    @Override
    public void handle(TrafficLight trafficLight) {
        System.out.println("Yellow Light: Be prepared to stop");
        trafficLight.setState(new RedState());
    }
}

class GreenState implements TrafficLightState {
    @Override
    public void handle(TrafficLight trafficLight) {
        System.out.println("Green Light: Go");
        trafficLight.setState(new YellowState());
    }
}

class TrafficLight {
    private TrafficLightState state;

    public TrafficLight() {
        state = new RedState();
    }

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

    public void change() {
        state.handle(this);
    }
}

public class TrafficLightTest {
    public static void main(String[] args) {
        TrafficLight trafficLight = new TrafficLight();

        for (int i = 0; i < 6; i++) {
            trafficLight.change();
        }
    }
}





image




image




posted @ 2023-12-02 10:38  qwertzxc  阅读(22)  评论(0)    收藏  举报