Java 实现状态(State)模式

状态模式(State)行为型
     根据内部状态的变化,改变对象的行为。既改变对象的状态,又改变对象的行为 


假定,一个游戏有五个基本的行为:open、login、prepare、game、exit

有四种基本状态:IdleState、OpenState、LoginState、GameState


提取行为,抽象出接口:

/**
 * author: stone
 */
public interface IGameState {

    void open();

    void login();

    void prepare();

    void game();

    void exit();

}

定义各个状态类, 分别实现该接口。

/**
 * author: stone
 */
public class IdleState implements IGameState {
    @Override
    public void open() {
        System.out.println("on IdleState, 打开了游戏");
    }

    @Override
    public void login() {
        System.out.println("on IdleState, 请打开游戏");
    }

    @Override
    public void prepare() {
        System.out.println("on IdleState, 请打开游戏");
    }

    @Override
    public void game() {
        System.out.println("on IdleState, 请打开游戏");
    }

    @Override
    public void exit() {
        System.out.println("on IdleState, 无效操作");
    }
}


/**
 * author: stone
 */
public class OpenState implements IGameState {
    @Override
    public void open() {
        System.out.println("on OpenState, 不能多开游戏");
    }

    @Override
    public void login() {
        System.out.println("on OpenState, 进行登录");
    }

    @Override
    public void prepare() {
        System.out.println("on OpenState, 请先登录");
    }

    @Override
    public void game() {
        System.out.println("on OpenState, 要先登录,才能玩游戏");
    }

    @Override
    public void exit() {
        System.out.println("on OpenState, 离开游戏");
    }
}


/**
 * author: stone
 */
public class LoginState implements IGameState {
    @Override
    public void open() {
        System.out.println("on LoginState, 不能多开游戏");
    }

    @Override
    public void login() {
        System.out.println("on LoginState, 不要重复登录");
    }

    @Override
    public void prepare() {
        System.out.println("on LoginState, 准备游戏人物、场景等");
    }

    @Override
    public void game() {
        System.out.println("on LoginState, 能玩游戏");
    }

    @Override
    public void exit() {
        System.out.println("on LoginState, 离开游戏");
    }
}

/**
 * author: stone
 */
public class GameState implements IGameState {
    @Override
    public void open() {
        System.out.println("on GameState, 不能多开游戏");
    }

    @Override
    public void login() {
        System.out.println("on GameState, 不要重复登录");
    }

    @Override
    public void prepare() {
        System.out.println("on GameState, 准备游戏人物、场景等");
    }

    @Override
    public void game() {
        System.out.println("on GameState, 游戏中");
    }

    @Override
    public void exit() {
        System.out.println("on GameState, 离开游戏");
    }
}

一个客户端类,集成状态的改变,与不同行为的处理。

/**
 * author: stone
 */
public class SuperGame {

    private IGameState state;
    private OpenState openState = new OpenState();
    private LoginState loginState = new LoginState();
    private GameState gameState = new GameState();
    private IdleState exitState = new IdleState();

    public SuperGame() {
        state = idleState;
    }

    public void open() {
        state.open();
        if (state == idleState) {
            state = openState;
        }
    }

    public void login() {
        state.login();
        if (state == openState) {
            state = loginState;
        }
    }

    private void prepare() {
        state.prepare();
    }

    public void play() {
        prepare();
        if (state == loginState) {
            state = gameState;
            state.game();
        }
    }

    public void exit() {
        state.exit();
        state = idleState;
    }
}

注意上面的play(),集成了状态类的 prepare()和game()


测试

public class Main {

    public static void main(String[] args) {

        SuperGame game = new SuperGame();
        game.open();
        game.login();
        game.play();
        game.exit();

        System.out.println("\n--- on IdleState, does not open ---");
        game.login();
        game.play();
        game.exit();

        System.out.println("\n--- on LoginState, does not login ---");
        game.open();
        game.play();
        game.exit();
    }
}

print:

on IdleState, 打开了游戏
on OpenState, 进行登录
on LoginState, 准备游戏人物、场景等
on GameState, 游戏中
on GameState, 离开游戏

--- on IdleState, does not open ---
on IdleState, 请打开游戏
on IdleState, 请打开游戏
on IdleState, 无效操作

--- on LoginState, does not login ---
on IdleState, 打开了游戏
on OpenState, 请先登录
on OpenState, 离开游戏


你可能感兴趣的:(Java,Android设计模式,设计模式(Java))