/// <summary>
/// 饮料基类
/// </summary>
public abstract class Beverage
{
protected string Description = "Unknown Beverage";
public virtual string GetDescription()
{
return Description;
}
public abstract double Cost();
}
/// <summary>
/// 浓缩咖啡
/// </summary>
public class Espresso : Beverage
{
public Espresso()
{
Description = "Espresso";
}
public override double Cost()
{
return 1.99f;
}
}
/// <summary>
///
/// </summary>
public class HouseBlend : Beverage
{
public HouseBlend()
{
Description = "HouseBlend";
}
public override double Cost()
{
return 0.89f;
}
}
/// <summary>
/// 调料
/// </summary>
public abstract class CondimentDecorator : Beverage
{
public void OtherMethod()
{
//
}
}
/// <summary>
/// 摩卡
/// </summary>
public class Mocha : CondimentDecorator
{
private Beverage _beverage;
public Mocha(Beverage beverage)
{
_beverage = beverage;
}
public override string GetDescription()
{
return _beverage.GetDescription() + ", Mocha";
}
public override double Cost()
{
return .2f + _beverage.Cost();
}
}
/// <summary>
///
/// </summary>
public class Whip : CondimentDecorator
{
private Beverage _beverage;
public Whip(Beverage beverage)
{
_beverage = beverage;
}
public override string GetDescription()
{
return _beverage.GetDescription() + ", Whip";
}
public override double Cost()
{
return .1f + _beverage.Cost();
}
}
---------------------------------------------------
void Test()
{
Beverage beverage = new Espresso();
Debug.Log(beverage.GetDescription() + " $" + beverage.Cost());
Beverage beverage1 = new Espresso();
beverage1 = new Mocha(beverage1);
beverage1 = new Mocha(beverage1);
beverage1 = new Whip(beverage1);
Debug.Log(beverage1.GetDescription() + " " + string.Format("{0:F4}", beverage1.Cost()));
}
《Head First 设计模式》