Kill()
,没有参数,好像你在命令忍者自杀。我会定义Ninja
对其他忍者采取行动:
public interface INinja
{
void KillBrutally(INinja otherNinja);
void KillHonorably(INinja otherNinja);
}
然后,提高从武器到杀招的抽象级别:
public interface IKillMove
{
void Kill(INinja ninja);
}
并让忍者支持杀戮类型:
public sealed class Ninja : INinja
{
private readonly IKillMove _brutalKillMove;
private readonly IKillMove _honorableKillMove;
public Ninja(IKillMove brutalKillMove, IKillMove honorableKillMove)
{
_brutalKillMove = brutalKillMove;
_honorableKillMove = honorableKillMove;
}
#region INinja
public void KillBrutally(INinja otherNinja)
{
_brutalKillMove.Kill(otherNinja);
}
public void KillHonorably(INinja otherNinja)
{
_honorableKillMove.Kill(otherNinja);
}
#endregion
}
现在我们可以担心武器了:
public interface IWeapon
{
void Attack(INinja ninja);
void Finish(INinja ninja);
}
并杀死动作:
public sealed class MoveInKillMove : IKillMove
{
private readonly IWeapon _shortRangeWeapon;
private readonly IWeapon _longRangeWeapon;
public MoveInKillMove(IWeapon shortRangeWeapon, IWeapon longRangeWeapon)
{
_shortRangeWeapon = shortRangeWeapon;
_longRangeWeapon = longRangeWeapon;
}
#region IKillMove
public void Kill(INinja ninja)
{
_longRangeWeapon.Attack(ninja);
_longRangeWeapon.Attack(ninja);
_longRangeWeapon.Attack(ninja);
_shortRangeWeapon.Finish(ninja);
}
#endregion
}
public sealed class FinishingMove : IKillMove
{
private readonly IWeapon _weapon;
public FinishingMove(IWeapon weapon)
{
_weapon = weapon;
}
#region IKillMove
public void Kill(INinja ninja)
{
_weapon.Finish(ninja);
}
#endregion
}
这是一个示例接线(根据需要转换为您的 IoC 容器):
var sword = new Sword();
var ninja = new Ninja(
new MoveInKillMove(sword, new Shuriken()),
new FinishingMove(sword));