using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp
{
class Program
{
public abstract class Beverage
{
protected string description = "Unknow Beverage";
virtual public string getDescription()
{
return description;
}
public abstract double cost();
}
public abstract class CondimentDecorator : Beverage
{
public abstract override string getDescription();
}
public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}
public override double cost()
{
return 1.99;
}
}
public class HouseBlend : Beverage
{
public HouseBlend()
{
description = "HouseBlend";
}
public override double cost()
{
return 0.89;
}
}
public class Mocha : CondimentDecorator
{
private Beverage beverage = null;
public Mocha(Beverage _beverage)
{
beverage = _beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Mocha";
}
public override double cost()
{
return beverage.cost() + 0.20;
}
}
static void Display_Beverage(Beverage beverage)
{
Console.WriteLine("{0} ${1}", beverage.getDescription(), beverage.cost());
}
public static void Test()
{
Beverage beverage1 = new Espresso();
Display_Beverage(beverage1);
Beverage beverage2 = new Mocha(beverage1);
Display_Beverage(beverage2);
}
static void Main(string[] args)
{
Test();
}
}
}
设计模式-装饰模式
最新推荐文章于 2024-08-03 23:17:20 发布