参考地址:
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...