- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Decker
- {
- class CRunMain
- {
- static void Main(string[] args)
- {
- CBeverage objBev = new CEspresso();
- CBeverage objBev2 = null;
- Console.WriteLine(objBev.GetDescription() + "$" + objBev.Cost());
- objBev2 = new CHouseBlend();
- objBev2 = new CMocha(objBev2);
- objBev2 = new CMocha(objBev2);
- objBev2 = new CMocha(objBev2);
- Console.WriteLine(objBev2.GetDescription() + "$" + objBev2.Cost());
- }
- };
- //饮料
- public abstract class CBeverage
- {
- public virtual string GetDescription()
- {
- return this.m_strDescription;
- }
- public abstract double Cost();
- protected string m_strDescription;
- };
- public abstract class CCondimentDecortor : CBeverage
- {
- public abstract string GetDescription();
- };
- public class CEspresso : CBeverage
- {
- public CEspresso()
- {
- this.m_strDescription = "Espresso";
- }
- public override double Cost()
- {
- return 1.99;
- }
- };
- public class CHouseBlend : CBeverage
- {
- public CHouseBlend()
- {
- this.m_strDescription = "House blend coffee";
- }
- public override double Cost()
- {
- return 0.89;
- }
- };
- //调料
- public class CMocha : CCondimentDecortor
- {
- public CMocha(CBeverage objBev)
- {
- this.m_objBev = objBev;
- this.m_strDescription = this.GetDescription();
- }
- public override string GetDescription()
- {
- return this.m_objBev.GetDescription() + ", Mocha";
- }
- public override double Cost()
- {
- if (null == this.m_objBev)
- {
- throw new Exception("null == obj");
- }
- return 0.2 + this.m_objBev.Cost();
- }
- private CBeverage m_objBev;
- };
- }
Decorator Pattern (装饰者模式)之星巴克的咖啡(C#源代码)
最新推荐文章于 2025-05-10 15:06:27 发布