[英]Nested Strategy Design Pattern
假设我们有一个玩家控制着一个名为Player1的角色。 为了在运行时启用武器切换,我有以下代码:
public interface IWeapon
{
void Fire();
}
public class Player1Weapon1 : IWeapon
{
...
void Fire()
{
//do actions of Weapon1
}
...
}
public class Player1Weapon2 : IWeapon
{
...
void Fire()
{
//do actions of Weapon2
}
...
}
public class Player1
{
IWeapon weapon;
Player1Weapon1 w1;
Player1Weapon2 w2;
public Player1()
{
w1 = new Player1Weapon1(this);
w2 = new Player1Weapon2(this);
SetWeapon(w1);
}
void Update()
{
if(SWITCH_BTN_HELD)
{
if(weapon.equals(w1)) SetWeapon(w2);
if(weapon.equals(w2)) SetWeapon(w1);
}
if(FIRE_BTN_HELD)
weapon.Fire();
}
void SetWeapon(w)
{
weapon = w;
}
}
那很好。
但是现在玩家可以选择另一个名为Player2的角色。 请注意,Player2的武器不同于Player1。 因此,我们可以添加以下类:
public class Player2Weapon1 : IWeapon
{
...
void Fire()
{
//do actions of Weapon1
}
...
}
public class Player2Weapon2 : IWeapon
{
...
void Fire()
{
//do actions of Weapon2
}
...
}
public class Player2
{
IWeapon weapon;
Player2Weapon1 w1;
Player2Weapon2 w2;
public Player2()
{
w1 = new Player2Weapon1(this);
w2 =new Player2Weapon2(this);
SetWeapon(w1);
}
void Update()
{
if(SWITCH_BTN_HELD)
{
if(weapon.equals(w1)) SetWeapon(w2);
if(weapon.equals(w2)) SetWeapon(w1);
}
if(FIRE_BTN_HELD)
weapon.Fire();
}
void SetWeapon(w)
{
weapon=w;
}
}
它会再次运行,但是非常紧,如果玩家想玩Player3,我应该在项目中添加更多类。
我想知道如何为劫掠者制作策略模式,如下所示:
interface IPlayer()
{
}
我不知道将哪些方法放在IPlayer中? 如何创建嵌套的策略设计模式?
不知道我是否知道您的要求,或者您是否不知道要问什么。 战略模式并不是您最需要的。 我试着给我两美分。
首先,我认为在玩家中拥有具体的类(例如Player1Weapon
不是一个好主意。 它应该只包含IWeapon
。 这样,您无需专门定义玩家使用的武器,也无需为每个变体创建新的玩家类别。
考虑以下。 您在SomeNamespace
命名空间中具有这些IWeapons
。
public interface IWeapon
{
void Fire();
}
public class Shotgun : IWeapon
{
public void Fire()
{
Console.WriteLine("Shotgun goes boom");
}
}
public class Knife : IWeapon
{
public void Fire()
{
Console.WriteLine("Stabbed teh sucker");
}
}
public class NuclearBomb : IWeapon
{
public void Fire()
{
Console.WriteLine("Game over for everyone!!1");
}
}
现在,您的Player
类可能如下所示。 只需添加您可能需要的任何抽象级别,在这里我假设您不需要。
public class Player
{
private IWeapon _wielded;
public Player(string name)
:this(name, null, null)
{}
public Player(string name, IWeapon primary, IWeapon secondary)
{
Name = name;
Primary = _wielded = primary;
Secondary = secondary;
Console.WriteLine(string.Format("Player '{0}' spawned", Name));
}
public void Switch()
{
_wielded = _wielded != Primary ? Primary : Secondary;
}
public void Fire()
{
if (_wielded != null)
_wielded.Fire();
}
public string Name { get; set; }
public IWeapon Primary { get; set; }
public IWeapon Secondary { get; set; }
}
要创建“任何播放器”,您可以有一个简单的工厂以所需的属性作为参数来“生成”它们。
public class PlayerFactory
{
// key = player name, value = weapons
public Player Create(KeyValuePair<string, string[]> args)
{
var primary = Activator.CreateInstance(Type.GetType(args.Value[0])) as IWeapon;
var secondary = Activator.CreateInstance(Type.GetType(args.Value[1])) as IWeapon;
var player = new Player(args.Key, primary, secondary);
return player;
}
}
现在,如果您运行以下“初始化” ...
// this would come from config file or similar
var config = new List<KeyValuePair<string, string[]>>
{
new KeyValuePair<string,string[]>(
"Player1", new[] { "SomeNamespace.Shotgun", "SomeNamespace.Knife" }),
new KeyValuePair<string,string[]>(
"Player2", new[] { "SomeNamespace.NuclearBomb", "SomeNamespace.Knife" })
};
var factory = new PlayerFactory();
foreach (var entry in config)
{
var player = factory.Create(entry);
player.Fire();
player.Switch();
player.Fire();
}
...您最终得到以下控制台日志
玩家“ Player1”产生
弹枪繁荣
刺吸盘玩家“ Player2”产生
每个人都结束游戏!! 1
刺吸盘
使用Strategy
模式时,您的设计可能类似于以下UML图。
Player1
将Fire()
操作委托给不同的武器类别Weapon1
, Weapon2
,...之一。
有关更多讨论,请参见http://w3sdesign.com上的“策略”设计模式。
public interface IWeapon
{
void Fire();
}
public class Weapon1 : IWeapon
{
...
void Fire()
{
//do actions of Weapon1
}
...
}
public class Weapon2 : IWeapon
{
...
void Fire()
{
//do actions of Weapon2
}
...
}
public interface IPlayer
{
void Update();
}
public class Player1 : IPlayer
{
private IWeapon weapon;
private IWeapon w1;
private IWeapon w2;
public Player1()
{
w1 = new Weapon1();
w2 = new Weapon2();
SetWeapon(w1);
}
void Update()
{
if(SWITCH_BTN_HELD)
{
if(weapon.equals(w1)) SetWeapon(w2);
if(weapon.equals(w2)) SetWeapon(w1);
}
if(FIRE_BTN_HELD)
weapon.Fire();
}
void SetWeapon(w)
{
weapon = w;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.