这个场景在RPG游戏中很常见. 下面我们使用装饰者模式去实现它.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorPatternDemo.com
{
/// <summary>
/// 所有加成战斗加成基类
/// </summary>
public abstract class BaseAddtition
{
/// <summary>
/// 被装饰者
/// </summary>
private BaseAddtition _decor = null;
/// <summary>
/// 本加成的power
/// </summary>
protected Int32 _power = 0;
/// <summary>
/// 战斗力描述
/// </summary>
protected string _describe = string.Empty;
/// <summary>
/// </summary>
/// <param name="describe">描述</param>
/// <param name="power">战斗力</param>
/// <param name="decor">被装饰者</param>
public BaseAddtition( string @describe, Int32 @power = 0 , BaseAddtition @decor = null)
{
this._describe = @describe;
this._power = @power;
this._decor = @decor;
}
/// <summary>
/// 统计战斗力
/// </summary>
/// <returns></returns>
public Int32 StatisticsPower()
{
if (null != this._decor)
{
return this._decor.StatisticsPower() + this._power;
}
else
{
return this._power;
}
}
/// <summary>
/// 获取描述
/// </summary>
public abstract string Describe { get; }
}
}
①:战靴
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorPatternDemo.com.cn
{
/// <summary>
/// 战靴加成战斗力
/// </summary>
public sealed class BootsAddtition : BaseAddtition
{
public BootsAddtition(string describe, Int32 power, BaseAddtition decor) : base(describe, power, decor)
{
}
public override string Describe
{
get { return string.Format(@"{0}{1}的战斗力:{2}", this._describe,"加成", this._power); }
}
}
}
②:护膝
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorPatternDemo.com.cn
{
/// <summary>
/// 护膝的加成战斗力
/// </summary>
public sealed class KneepadAddtition : BaseAddtition
{
public KneepadAddtition(string describe, Int32 power, BaseAddtition decor) : base(describe, power, decor)
{
}
public override string Describe
{
get { return string.Format(@"{0}{1}的战斗力:{2}", this._describe,"加成", this._power); }
}
}
}
三 : 主角
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorPatternDemo.com.cn
{
/// <summary>
/// 主角的基本战斗力
/// </summary>
public sealed class LeadBasePower : BaseAddtition
{
public LeadBasePower(string describe, Int32 power) : base(describe , power, null )
{ }
public override string Describe
{
get { return string.Format(@"{0}{1}的战斗力:{2}", this._describe , "基础", this._power); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DecoratorPatternDemo.com;
using DecoratorPatternDemo.com.cn;
namespace DecoratorPatternDemo
{
class Program
{
static void Main(string[] args)
{
BaseAddtition @leader = new LeadBasePower(@"主角", 100000);
Console.WriteLine(@leader.Describe);
//用战靴来装饰主角
BaseAddtition @boots = new BootsAddtition(@"战靴", 12, @leader);
Console.WriteLine(@boots.Describe);
//用护膝装饰战靴(间接装饰主角)
BaseAddtition @kneepad = new KneepadAddtition(@"护膝", 200, @boots);
Console.WriteLine(@kneepad.Describe);
//计算综合战斗力
Console.WriteLine(@"总战斗力 : {0}", @kneepad.StatisticsPower());
Console.Read();
}
}
}
注意层层装饰 , 递归调用
?????为何需要考虑装饰者模式 : 当加入新的装备后 ???? 可以直接继承BaseAddtition抽象类.实现了开闭原则.
原文地址:http://blog.51cto.com/aonaufly/2093921