该类图近日网上
装饰器模式主要组成部分/// <summary>
/// 相当于Component
/// </summary>
public abstract class YinLiao
{
public abstract double Cast();
}
/// <summary>
/// 相当于ConcreteComponent
/// </summary>
public class MilkTea : YinLiao
{
public override double Cast()
{
Console.WriteLine("奶茶10块钱一杯");
return 10;
}
}
/// <summary>
/// 相当于ConcreteComponent
/// </summary>
public class FruitTea : YinLiao
{
public override double Cast()
{
Console.WriteLine("水果茶15块钱一杯");
return 15;
}
}
/// <summary>
/// 相当于ConcreteComponent
/// </summary>
public class SodaTea : YinLiao
{
public override double Cast()
{
Console.WriteLine("苏打水4块钱一杯!");
return 4;
}
}
/// <summary>
/// 装饰器模式第壹个核心
/// </summary>
public abstract class Decorator : YinLiao
{
/// <summary>
/// 添加一个父类得引用
/// </summary>
private YinLiao yinLiao;
/// <summary>
/// 通过set方法赋值
/// </summary>
/// <param name="yinLiao"></param>
public void SetComponent(YinLiao yinLiao)
{
this.yinLiao = yinLiao;
}
public override double Cast()
{
return this.yinLiao.Cast();
}
}
/// <summary>
/// 相当于ConcreteDecorator
/// </summary>
public class BuDing : Decorator
{
private static double money = 5;
public override double Cast()
{
Console.WriteLine("布丁5块");
//第二个核心
return base.Cast() + money;
}
}
/// <summary>
/// 相当于ConcreteDecorator
/// </summary>
public class Zhenzhu : Decorator
{
private static double money = 7;
public override double Cast()
{
Console.WriteLine("珍珠7块");
//第二个核心
return base.Cast() + money;
}
}
/// <summary>
/// 相当于ConcreteDecorator
/// </summary>
public class XianCao : Decorator
{
private static double money = 6;
public override double Cast()
{
Console.WriteLine("仙草6块");
//第二个核心
return base.Cast() + money;
}
}
//C#控制台调用
Console.WriteLine("装饰器设计模式!");
MilkTea milkTea = new MilkTea();
BuDing buDing1 = new BuDing();
BuDing buDing2 =new BuDing();
Zhenzhu zhenzhu = new Zhenzhu();
buDing1.SetComponent(milkTea);
buDing2.SetComponent(buDing1);
zhenzhu.SetComponent(buDing2);
Console.WriteLine(zhenzhu.Cast());