装饰模式

参考:https://www.cnblogs.com/stonefeng/p/5679638.html

  公司门口有一个小摊卖手抓饼和肉夹馍的,有时候中午不想吃饭就会去光顾一下那个小摊,点了手抓饼之后往往还可以在这个基础之上增加一些配料,例如煎蛋,火腿片等等,每个配料的价格都不一样,不管你怎么配配料,最终价格是手抓饼基础价加上每一种所选配料价格的总和。小摊的价格单如下:

装饰模式

 

代码:

贩卖食物的具体对象类:

 /// <summary>
    /// 贩卖的食物具体对象
    /// </summary>
    public class FoodComponent
    {
        public string FoodName = "";
        protected double m_Price;
        public FoodComponent(string foodName, double price)
        {
            this.FoodName = foodName;
            this.m_Price = price;
        }
        /// <summary>
        /// 购买东西的描述
        /// </summary>
        /// <returns></returns>
        public virtual string GetFoodDesc()
        {
            return this.FoodName;
        }

        public virtual double GetPrice()
        {
            return m_Price;
        }
    }

  加料装饰抽象对象:

 /// <summary>
    /// 加料装饰抽象对象
    /// </summary>
    public abstract class Decorator : FoodComponent
    {
        protected FoodComponent m_Component;
        public Decorator(string foodName, double price)
            : base(foodName, price)
        {

        }
        /// <summary>
        /// 增加装饰对象
        /// </summary>
        /// <param name="component"></param>
        public void SetComponent(FoodComponent component)
        {
            if (component != null)
            {
                this.m_Component = component;
                Console.WriteLine("--加{0}一个,¥{1}元", base.FoodName, base.m_Price);
                this.FoodName = this.m_Component.FoodName + ",加 " + base.FoodName;
            }
        }

        public override double GetPrice()
        {
            if (m_Component == null)
            {
                return 0;
            }
            return this.m_Price;
        }
    }

  具体的加料对象:

/// <summary>
    /// 煎蛋
    /// </summary>
    public class FireEgg : Decorator
    {
        public FireEgg(string foodName = "煎蛋", double price = 2)
            : base(foodName, price)
        {
        }

        public override double GetPrice()
        {
            return m_Component.GetPrice() + this.m_Price;//加料后价格要累加
        }
    }
    /// <summary>
    /// 火腿片
    /// </summary>
    public class Ham : Decorator
    {
        public Ham(string foodName = "火腿片", double price = 1.5)
            : base(foodName, price)
        {

        }

        public override double GetPrice()
        {
            return m_Component.GetPrice() + this.m_Price;
        }
    }
    /// <summary>
    /// 肉松
    /// </summary>
    public class MeatFloss : Decorator
    {
        public MeatFloss(string foodName = "肉松", double price = 1)
            : base(foodName, price)
        {

        }

        public override double GetPrice()
        {
            return m_Component.GetPrice() + this.m_Price;
        }
    }
    /// <summary>
    /// 黄瓜丝
    /// </summary>
    public class Cucumber : Decorator
    {
        public Cucumber(string foodName = "黄瓜丝", double price = 0.5)
            : base(foodName, price)
        {

        }

        public override double GetPrice()
        {
            return m_Component.GetPrice() + this.m_Price;
        }
    }

  调用方法:

  

  static void Main(string[] args)
        {
            FoodComponent tornCake = new FoodComponent("手抓饼", 4);
            Console.WriteLine("贩卖:{0},¥{1}元", tornCake.FoodName, tornCake.GetPrice());
            var hum1 = new Ham();//1.5
            hum1.SetComponent(tornCake);
            System.Console.WriteLine("\r\n汇总:{0} \r\n合计:¥{1}元\r\n", hum1.GetFoodDesc(), hum1.GetPrice());


            FoodComponent roujiamo = new FoodComponent("肉夹馍", 6);//6
            Console.WriteLine("贩卖:{0},¥{1}元", roujiamo.FoodName, roujiamo.GetPrice());
            var fireEgg1 = new FireEgg();//2
            fireEgg1.SetComponent(roujiamo);

            var fireEgg2 = new FireEgg();//2
            fireEgg2.SetComponent(fireEgg1);

            var hum = new Ham();//1.5
            hum.SetComponent(fireEgg2);

            var meatFloss = new MeatFloss();//肉松 1
            meatFloss.SetComponent(hum);

            var cucumber1 = new Cucumber();//黄瓜丝 0.5
            cucumber1.SetComponent(meatFloss);

            var cucumber2 = new Cucumber();//黄瓜丝 0.5
            cucumber2.SetComponent(cucumber1);
            //我好饿
            System.Console.WriteLine("\r\n汇总:{0} \r\n合计:¥{1}元\r\n", cucumber2.GetFoodDesc(), cucumber2.GetPrice());
            Console.Read();
        }

  

  效果:

装饰模式

 

上一篇:开闭原则


下一篇:react源码解析2.react的设计理念