设计模式--装饰者模式
装饰模式:顾名思义是用某一个物体来装饰某一个主体物体来形成新的物体。我个人觉得更趋向于由一个主体物品和若干个装饰物品来构成新的物品。比如
一杯纯咖啡(主体),而糖,奶油 属于装饰者.----->奶油咖啡
代码
#region 装饰者模式
public abstract class Beverage
{
protected string strDescription = "UnKnow Beverage";
public string getDescription()
{
return strDescription;
}
public abstract double cost();
}
public abstract class BeverageDecorate:Beverage
{
public abstract string getDescription();
}
public class Mocha : BeverageDecorate
{
Beverage _Beverage;
public Mocha(Beverage obj)
{
_Beverage = obj;
}
public override string getDescription()
{
return _Beverage.getDescription() + ", Mocha";
}
public override double cost()
{
return 0.9 + this._Beverage.cost();
}
}
public class Espresso : Beverage
{
public Espresso() {
strDescription = "Espresso";
}
public override double cost()
{
return 1.99;
}
}
#endregion
public abstract class Beverage
{
protected string strDescription = "UnKnow Beverage";
public string getDescription()
{
return strDescription;
}
public abstract double cost();
}
public abstract class BeverageDecorate:Beverage
{
public abstract string getDescription();
}
public class Mocha : BeverageDecorate
{
Beverage _Beverage;
public Mocha(Beverage obj)
{
_Beverage = obj;
}
public override string getDescription()
{
return _Beverage.getDescription() + ", Mocha";
}
public override double cost()
{
return 0.9 + this._Beverage.cost();
}
}
public class Espresso : Beverage
{
public Espresso() {
strDescription = "Espresso";
}
public override double cost()
{
return 1.99;
}
}
#endregion