public abstract class ProductBase
{
public abstract string GetName();
public abstract double GetPrice();
}
public class ConcretProuct : ProductBase
{
private string _name;
private double _price;
public ConcretProuct(string name, double price)
{
this._name = name;
this._price = price;
}
public override string GetName()
{
return _name;
}
public override double GetPrice()
{
return _price;
}
}
public class Decorator : ProductBase
{
private ProductBase _product = null;
private string _name;
private double _price;
public Decorator(ProductBase product, string name, double price)
{
this._product = product;
this._name = name;
this._price = price;
}
public override string GetName()
{
return string.Format("{0},{1}", _product.GetName(), _name);
}
public override double GetPrice()
{
return _product.GetPrice() + _price;
}
}
class Program
{
static void Main(string[] args)
{
ConcretProuct livingProduct = new ConcretProuct("平底锅",100);
Console.WriteLine(PrintProductDetails(livingProduct));
Decorator dec1 = new Decorator(livingProduct,"海鲜酱油",10);
Console.WriteLine(PrintProductDetails(dec1));
Decorator dec2 = new Decorator(dec1, "老坛酸菜",12);
Console.WriteLine(PrintProductDetails(dec2));
Console.ReadKey();
}
private static string PrintProductDetails(ProductBase product)
{
return string.Format("产品组合:{0} 价格:{1}", product.GetName(), product.GetPrice());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有