【设计模式】之状态模式

版权声明:欢迎转载,但是请附上原文链接! https://blog.csdn.net/zixiao217/article/details/84553517

状态设计模式


状态模式属于行为模式,基于当前状态处理运行时对象的行为。

当一个对象内部状态发生变更时,允许该对象改变其行为。对象可能会显示地更改其类。

状态设计模式是怎样工作的

  • 定义一个表示多种状态的对象,该对象可以成为一个记忆状态机。
  • 定义一个上下文对象,行为随其状态而变化。

用例

状态模式在游戏开发中是一种经常使用的设计模式。
游戏角色体征可能存在不同的状态如:健康(绿色、能飞能跑)、虚弱(黄色,能走)、死亡;
当体征为健康时,允许角色可以使用不同的武器攻击敌人。
当体征为虚弱时,健康濒临一个临界值。
当体征值达到0时,即角色死亡游戏结束。

编码实现

Player.java

Player 类定义了一个玩家不同的动作。

package org.byron4j.cookbook.designpattern.state;

public class Player {
    public void attack(){
        System.out.println("Attack");
    }

    public void fireBumb() {
        System.out.println("Fire Bomb");
    }

    public void fireGunblade() {
        System.out.println("Fire Gunblade");
    }

    public void fireLaserPistol() {
        System.out.println("Laser Pistols");
    }

    public void firePistol() {
        System.out.println("Fire Pistol");
    }

    public void survive() {
        System.out.println("Surviving!");
    }

    public void dead() {
        System.out.println("Dead! Game Over");
    }
}


GameComtext.java

定义了不同的动作其条件依赖于player的状态。

package org.byron4j.cookbook.designpattern.state;

public class GameContext {
    private Player player = new Player();

    public void gameAction(String state) {
        if (state == "healthy") {
            player.attack();
            player.fireBumb();
            player.fireGunblade();
            player.fireLaserPistol();
        } else if (state == "survival") {
            player.survive();
            player.firePistol();
        } else if (state == "dead") {
            player.dead();
        }
    }
}


上面的代码快照, gameAction 方法包含了很多条件块去表现不同的动作基于player的当前状态。
但是是一堆很难维护的代码。,我们可以使用状态模式来避免该问题。

状态设计模式实例

状态模式类图

1. 定义一个 PlayerState 接口,定义了需要传入 Player 类的实例的动作方法

public interface PlayerState {	
	void action(Player p);
}
2. 定义三个不同的类表示不同的状态

均实现了 PlayerState 接口,提供了指定的 action() 方法的实现。

public class HealthyState implements PlayerState {

	@Override
	public void action(Player p) {
		p.attack();
		p.fireBumb();
		p.fireGunblade();
		p.fireLaserPistol();
	}
}

public class SurvivalState implements PlayerState {

	@Override
	public void action(Player p) {
		p.survive();
		p.firePistol();
	}
}

public class DeadState implements PlayerState {

	@Override
	public void action(Player p) {
		p.dead();
	}
}

3. GameContxt 类包含设置状态的方法组合。

public class GameContext {
	
	private PlayerState state = null;
	private Player player = new Player();

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

	public void gameAction() {
		state.action(player);
	}
}

设计模式测试用例

public class GameTest {

	public static void main(String[] args) {

		GameContext context = new GameContext();

		context.setState(new HealthyState());
		context.gameAction();
		System.out.println("*****");

		context.setState(new SurvivalState());
		context.gameAction();
		System.out.println("*****");

		context.setState(new DeadState());
		context.gameAction();
		System.out.println("*****");

	}
}

输出为:

Attack
Fire Bomb
Fire Gunblade
Laser Pistols
*****
Surviving!
Fire Pistol
*****
Dead! Game Over
*****

Process finished with exit code 0


猜你喜欢

转载自blog.csdn.net/zixiao217/article/details/84553517
今日推荐