• 设计模式之装饰模式C#版本


    参考: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("
    汇总:{0} 
    合计:¥{1}元
    ", 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("
    汇总:{0} 
    合计:¥{1}元
    ", cucumber2.GetFoodDesc(), cucumber2.GetPrice());
                Console.Read();
            }
    

      

      效果:

  • 相关阅读:
    用python一键爬取几千张表情包斗图,分分钟征服朋友圈所有好友
    Python新手必备的15个字符串方法,你学废了吗?
    价值10K+的Python面试题,珍藏已久,分享给大家!
    用python开发一个益智游戏,没事就锻炼锻炼自己的方向感
    室友吃个泡面的时间,我就用Python代码下载了几千张手机壁纸,简直yyds!
    Python GUI编程:tkinter 关于 ttkbootstrap 的使用
    .NET 6 使用 System.Drawing.Common 出现 The type initializer for ‘Gdip’ threw an exception 异常的解决办法
    如何在 Linux 上使用 NPOI
    Silverlight企业应用快速开发平台框架设计(二)分析平台实现重点是什么,要怎么做?
    Silverlight企业应用快速开发平台框架设计(一)目标
  • 原文地址:https://www.cnblogs.com/luofuxian/p/10337973.html
Copyright © 2020-2023  润新知