游戏开发中的设计模式之一-Strategy模式
1. 定义
Strategy模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
2. 设计原则
找出应用中可能需要变化之处,把它们独立出来,不要和哪些不需要变化的代码混在一起
3. 实例
我们在游戏中经常看到有不同的角色,各种角色使用不同的武器,并且还可以更换武器。该问题可以抽象为如下描述:游戏角色类及武器行为类,每个角色一次只能使用一个武器,但是可以在游戏过程中更换武器。
通过分析,该应用可以使用Strategy模式解决,并给出类图如下,类图使用Rose逆向生成的,有点乱:)图片比较大,如果显示不完整,大家可以保存到本地打开看:
4. 代码实现
/*****************Strategy.h**************************/
- // ***************************************************************
- // Strategy version: 1.0 · date: 09/01/2008
- // -------------------------------------------------------------
- // Author: Wang Liuqiang
- // E-mail: wlq_729@163.com
- // -------------------------------------------------------------
- // Copyright (C) 2008 - All Rights Reserved
- // ***************************************************************
- #ifndef STRATEGY_H_
- #define STRATEGY_H_
- class WeaponBehavior;
- class Character
- {
- public:
- Character();
- Character(WeaponBehavior* pWeapon);
- ~Character();
- void fight();
- void SetWeapon(WeaponBehavior* w);
- private:
- WeaponBehavior* m_weapor;
- };
- class Queue : public Character
- {
- public:
- Queue();
- ~Queue();
- };
- class King : public Character
- {
- public:
- King();
- ~King();
- };
- class Troll : public Character
- {
- public:
- Troll();
- ~Troll();
- };
- class Knight : public Character
- {
- public:
- Knight();
- ~Knight();
- };
- class WeaponBehavior
- {
- public:
- WeaponBehavior();
- ~WeaponBehavior();
- virtual void useWeapon() = 0;
- };
- class KnifeBehvior : public WeaponBehavior
- {
- public:
- KnifeBehvior();
- ~KnifeBehvior();
- virtual void useWeapon();
- };
- class BowAndArrorBehavior : public WeaponBehavior
- {
- public:
- BowAndArrorBehavior();
- ~BowAndArrorBehavior();
- virtual void useWeapon();
- };
- class AxeBehavior : public WeaponBehavior
- {
- public:
- AxeBehavior();
- ~AxeBehavior();
- virtual void useWeapon();
- };
- class SwordBehavior : public WeaponBehavior
- {
- public:
- SwordBehavior();
- ~SwordBehavior();
- virtual void useWeapon();
- };
- #endif
/*********************Strategy.cpp*****************************/
- #include "Strategy.h"
- #include <iostream>
- using namespace std;
- Character::Character():m_weapor(NULL)
- {
- }
- Character::Character(WeaponBehavior* pWeapon)
- {
- m_weapor = pWeapon;
- }
- Character::~Character()
- {
- if (m_weapor != NULL)
- {
- delete m_weapor;
- m_weapor = NULL;
- }
- }
- void Character::fight()
- {
- m_weapor->useWeapon();
- }
- void Character::SetWeapon(WeaponBehavior* w)
- {
- m_weapor = w;
- }
- Queue::Queue():Character(new KnifeBehvior())
- {
- }
- Queue::~Queue()
- {
- }
- King::King():Character(new SwordBehavior())
- {
- }
- King::~King()
- {
- }
- Troll::Troll():Character(new AxeBehavior())
- {
- }
- Knight::Knight():Character(new BowAndArrorBehavior())
- {
- }
- WeaponBehavior::WeaponBehavior()
- {
- }
- WeaponBehavior::~WeaponBehavior()
- {
- }
- KnifeBehvior::KnifeBehvior()
- {
- }
- KnifeBehvior::~KnifeBehvior()
- {
- }
- void KnifeBehvior::useWeapon()
- {
- cout<<"Fight use Knife!"<<endl;
- }
- AxeBehavior::AxeBehavior()
- {
- }
- AxeBehavior::~AxeBehavior()
- {
- }
- void AxeBehavior::useWeapon()
- {
- cout<<"Fight use Axe!"<<endl;
- }
- SwordBehavior::SwordBehavior()
- {
- }
- SwordBehavior::~SwordBehavior()
- {
- }
- void SwordBehavior::useWeapon()
- {
- cout<<"Fight use Sword!"<<endl;
- }
- BowAndArrorBehavior::BowAndArrorBehavior()
- {
- }
- BowAndArrorBehavior::~BowAndArrorBehavior()
- {
- }
- void BowAndArrorBehavior::useWeapon()
- {
- cout<<"Fight use BowAndArrow!"<<endl;
- }
/********************Main.cpp****************************/
- #include "Strategy.h"
- void main()
- {
- Character* character = new King();
- character->fight();
- character = new Queue();
- character->fight();
- character = new Troll();
- character->fight();
- character = new Knight();
- character->fight();
- WeaponBehavior* pWeapon = new SwordBehavior();
- character->SetWeapon(pWeapon);
- character->fight();
- delete character;
- character = 0;
- }