C++实现策略(Strategy)模式
来源:互联网 发布:lols8 知乎 编辑:程序博客网 时间:2024/04/29 06:47
策略(Strategy)模式:它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到算法的客户。下面模拟游戏中常用的打斗场面,来理解策略模式。在游戏中,通常有n个角色,每个角色都能发出动作:fight。每个角色都使用不同的武器,并且可以更换武器。下面是此场景简化的C++实现:
Strategy.h
说明:
1.因为每个武器的使用方式不同,所以将使用武器的行为封装成接口,利用此接口调用具体武器的useWeapon行为。
2.因为每个角色都有打斗,更换武器的行为,各个角色的打斗行为不同,而更换武器的行为相同,所以将setWeapon在Character中实现,而打斗行为在各个具体的角色中实现。
Strategy.cpp
main.cpp
策略(Strategy)模式体现了2个重要的面向对象设计原则:
1.封装变化。
2.针对接口编程。
Strategy.h
#ifndef _STRETEGY_H_
#define _STRETEGY_H_
//定义武器接口
class WeaponBehavior
{
public:
void virtual useWeapon() = 0;
};
class Sword:public WeaponBehavior
{
public:
void useWeapon();
};
class Axe:public WeaponBehavior
{
public:
void useWeapon();
};
class Arrow:public WeaponBehavior
{
public:
void useWeapon();
};
class Knife:public WeaponBehavior
{
public:
void useWeapon();
};
//定义角色接口
class Character
{
public:
Character()
{
weapon = 0;
}
void setWeapon(WeaponBehavior *w)
{
this->weapon = w;
}
void virtual fight() = 0;
protected:
WeaponBehavior *weapon;
};
class King:public Character
{
public:
void fight();
};
class Queen:public Character
{
public:
void fight();
};
class Knight:public Character
{
public:
void fight();
};
class Troll:public Character
{
public:
void fight();
};
#endif
#define _STRETEGY_H_
//定义武器接口
class WeaponBehavior
{
public:
void virtual useWeapon() = 0;
};
class Sword:public WeaponBehavior
{
public:
void useWeapon();
};
class Axe:public WeaponBehavior
{
public:
void useWeapon();
};
class Arrow:public WeaponBehavior
{
public:
void useWeapon();
};
class Knife:public WeaponBehavior
{
public:
void useWeapon();
};
//定义角色接口
class Character
{
public:
Character()
{
weapon = 0;
}
void setWeapon(WeaponBehavior *w)
{
this->weapon = w;
}
void virtual fight() = 0;
protected:
WeaponBehavior *weapon;
};
class King:public Character
{
public:
void fight();
};
class Queen:public Character
{
public:
void fight();
};
class Knight:public Character
{
public:
void fight();
};
class Troll:public Character
{
public:
void fight();
};
#endif
说明:
1.因为每个武器的使用方式不同,所以将使用武器的行为封装成接口,利用此接口调用具体武器的useWeapon行为。
2.因为每个角色都有打斗,更换武器的行为,各个角色的打斗行为不同,而更换武器的行为相同,所以将setWeapon在Character中实现,而打斗行为在各个具体的角色中实现。
Strategy.cpp
#include <iostream>
#include "Strategy.h"
using namespace std;
void Sword::useWeapon()
{
cout << "Use Sword to stuck!" << endl;
}
void Axe::useWeapon()
{
cout << "Use Axe to chop!" << endl;
}
void Knife::useWeapon()
{
cout << "Use Knife to kill!" << endl;
}
void Arrow::useWeapon()
{
cout << "Use arrow!" << endl;
}
void King::fight()
{
cout << "The king:" ;
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
void Queen::fight()
{
cout << "The Queen:" ;
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
void Knight::fight()
{
cout << "The Knight:" ;
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
void Troll::fight()
{
cout << "The Troll:";
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
#include "Strategy.h"
using namespace std;
void Sword::useWeapon()
{
cout << "Use Sword to stuck!" << endl;
}
void Axe::useWeapon()
{
cout << "Use Axe to chop!" << endl;
}
void Knife::useWeapon()
{
cout << "Use Knife to kill!" << endl;
}
void Arrow::useWeapon()
{
cout << "Use arrow!" << endl;
}
void King::fight()
{
cout << "The king:" ;
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
void Queen::fight()
{
cout << "The Queen:" ;
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
void Knight::fight()
{
cout << "The Knight:" ;
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
void Troll::fight()
{
cout << "The Troll:";
if ( this->weapon == NULL)
{
cout << "You don't have a weapon! Please Set Weapon!" << endl;
}
else
{
weapon->useWeapon();
}
}
main.cpp
#include <iostream>
#include "Strategy.h"
using namespace std;
int main()
{
//声明武器
WeaponBehavior *sw = new Sword();//声明剑
WeaponBehavior *axe = new Axe();//声明斧头
WeaponBehavior *arr = new Arrow();//声明弓箭
WeaponBehavior *kn = new Knife();//声明刀
//声明角色
Character *kin = new King();
Character *qu = new Queen();
Character *kni = new Knight();
Character *tr = new Troll();
//调用打斗行为
kin->fight();
qu->fight();
kni->fight();
tr->fight();
cout << endl;
//更换武器
kin->setWeapon(sw);
kin->fight();
cout << endl;
kin->setWeapon(arr);
kin->fight();
return 0;
}
#include "Strategy.h"
using namespace std;
int main()
{
//声明武器
WeaponBehavior *sw = new Sword();//声明剑
WeaponBehavior *axe = new Axe();//声明斧头
WeaponBehavior *arr = new Arrow();//声明弓箭
WeaponBehavior *kn = new Knife();//声明刀
//声明角色
Character *kin = new King();
Character *qu = new Queen();
Character *kni = new Knight();
Character *tr = new Troll();
//调用打斗行为
kin->fight();
qu->fight();
kni->fight();
tr->fight();
cout << endl;
//更换武器
kin->setWeapon(sw);
kin->fight();
cout << endl;
kin->setWeapon(arr);
kin->fight();
return 0;
}
策略(Strategy)模式体现了2个重要的面向对象设计原则:
1.封装变化。
2.针对接口编程。
- C++实现策略(Strategy)模式
- Java 实现策略(Strategy)模式
- 策略(Strategy)模式(C#实现版本)
- 设计模式C++(Strategy策略模式)
- 设计模式C++(Strategy策略模式)
- C++设计模式实现--策略(Strategy)模式
- 策略模式 Strategy模式
- Strategy模式 策略模式
- 策略模式(Strategy模式)
- 策略模式(Strategy Pattern)
- Strategy策略模式
- 策略(Strategy)模式
- Strategy策略模式
- 策略(Strategy)模式
- 策略模式(Strategy Pattern)
- 策略模式 Strategy Pattern
- 策略模式(Strategy Pattern)
- 策略模式(Strategy)
- Asp.net中防止用户多次登录的方法--希望大家跟我谈论
- Observer Component in Cairngorm
- Rails源代码分析(11):ActionController::Layout(2)
- 谁有口袋西游完整客户端给我发一个.........
- blogger绑定主机列表
- C++实现策略(Strategy)模式
- 做人做事的十个技巧
- 关于OpenCV_1.1pre1a 安装若干问题的解决方法:
- session.get()和session.load()的区别
- 我们需要“梦想比回忆多”的精神
- 正则表达式
- 8086汇编模拟器
- 秦淮八艳
- 中大印象