状态模式

参考地址:

http://blog.csdn.net/lmj623565791/article/details/26350617


看了这篇文章之后,想起来一个自己写过的蛋疼的逻辑,现在实现。

状态:

1, 游戏准备

2, 游戏中

3, 暂停中

4, 游戏结束

 

行为:

1, 开始游戏

2, 暂停游戏

3, 继续游戏

4, 结束游戏

5, 准备游戏


状态模式下,根据不同的状态对不同的行为做出不同的响应。


接口:

package statemode;

public interface State {
	public void startGame();
	public void stopGame();
	public void resumeGame();
	public void finishGame();
	public void readyGame();
}

分别实现不同的状态:

准备状态:

package statemode;

public class ReadyState implements State {

	private Game game;

	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	public ReadyState(Game game) {
		this.game = game;
	}

	@Override
	public void startGame() {
		System.out.println("start game...");
		this.game.setCurrentState(game.getPlayingState());
	}

	@Override
	public void stopGame() {
		System.out.println("game is ready!can not stop!");
	}

	@Override
	public void resumeGame() {
		System.out.println("game is ready!can not resume!");
	}

	@Override
	public void finishGame() {
		System.out.println("game is ready!can not finish!");
	}

	@Override
	public void readyGame() {
		System.out.println("game is ready!can not restart!");
	}
}

游戏中:

package statemode;

public class PlayingState implements State {

	private Game game;
	
	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}
	
	public PlayingState(Game game) {
		super();
		this.game = game;
	}

	@Override
	public void startGame() {
		System.out.println("game is playing now!cannot start game!");
	}

	@Override
	public void stopGame() {
		System.out.println("stop game...");
		game.setCurrentState(game.getStoppingState());
	}
	
	@Override
	public void resumeGame() {
		System.out.println("game is playing now!cannot resume game!");
	}

	@Override
	public void finishGame() {
		System.out.println("finish game...");
		game.setCurrentState(game.getOverState());
	}

	@Override
	public void readyGame() {
		System.out.println("game is playing now!cannot restart game!");
	}

}

游戏暂停:

package statemode;

public class StoppingState implements State {

	private Game game;

	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	public StoppingState(Game game) {
		super();
		this.game = game;
	}

	@Override
	public void startGame() {
		System.out.println("game is stopping!can not start game!");
	}

	@Override
	public void stopGame() {
		System.out.println("game is already stopped!can not stop game!");
	}

	@Override
	public void resumeGame() {
		System.out.println("resume game...");
		game.setCurrentState(game.getPlayingState());
	}

	@Override
	public void finishGame() {
		System.out.println("finish game...");
		game.setCurrentState(game.getOverState());
	}

	@Override
	public void readyGame() {
		System.out.println("game is stopping!can not restart game!");
	}

}

游戏结束:

package statemode;

public class OverState implements State {

	private Game game;
	
	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}
	
	public OverState(Game game) {
		super();
		this.game = game;
	}

	@Override
	public void startGame() {
		// TODO Auto-generated method stub
		System.out.println("game is over!can not start game!");
	}

	@Override
	public void stopGame() {
		// TODO Auto-generated method stub
		System.out.println("game is over!can not stop game!");
	}

	@Override
	public void resumeGame() {
		// TODO Auto-generated method stub
		System.out.println("game is over!can not resume game!");
	}

	@Override
	public void finishGame() {
		// TODO Auto-generated method stub
		System.out.println("game is over!can not finish game!");
	}

	@Override
	public void readyGame() {
		System.out.println("ready game...");
		game.setCurrentState(game.getReadyState());
	}
}

接下来是游戏类的实现:

package statemode;

public class Game {
	private State readyState;
	private State playingState;
	private State stoppingState;
	private State overState;

	private State currentState;

	public void setCurrentState(State currentState) {
		this.currentState = currentState;
	}

	public State getOverState() {
		return overState;
	}

	public State getPlayingState() {
		return playingState;
	}

	public State getReadyState() {
		return readyState;
	}

	public State getStoppingState() {
		return stoppingState;
	}

	public Game() {
		readyState = new ReadyState(this);
		playingState = new PlayingState(this);
		stoppingState = new StoppingState(this);
		overState = new OverState(this);
		currentState = readyState;
	}

	public void start() {
		currentState.startGame();
	}

	public void stop() {
		currentState.stopGame();
	}

	public void resume() {
		currentState.resumeGame();
	}

	public void finish() {
		currentState.finishGame();
	}

	public void reStart() {
		currentState.readyGame();
	}
}

最后是测试类:

package statemode;

public class TestMain {
	public static void main(String[] args) {
		Game game = new Game();
		game.start();
		game.stop();
		game.resume();
		game.finish();
		game.reStart();
	}
}

结果:

start game...
stop game...
resume game...
finish game...
ready game...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值