• 用C#改写Head First Design PatternsDecorator装饰(原创)


    饮料的包装

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Decorator
    {
        //抽象组件类 饮料
        public abstract class Beverage
        {
            public string description = "未知描述";
            public Size size;
            public abstract Size getsize();
            public abstract string getDescription();
            public abstract double cost();
        }
       
        //咖啡类
        public class Coffe : Beverage
        {
            public override Size getsize()
            {
                return size;
            }

            public Coffe(Size s)
            {

                size = s;
                if (this.getsize() == Size.Big)
                {
                    description = "大Coffe";
                }
                if (this.getsize() == Size.Mid)
                {
                    description = "中Coffe";
                }
                if (this.getsize() == Size.Small)
                {
                    description = "小Coffe";
                }
            }

            public override string getDescription()
            {
                return this.description;
            }


            public override double  cost()
            {
                double d = 0;
                if (this.getsize() == Size.Big)
                {
                    d = 4.5;
                }
                if (this.getsize() == Size.Mid)
                {
                    d = 3.5;
                }
                if (this.getsize() == Size.Small)
                {
                    d = 2.5;
                }
                return d;
            }
        }

        //茶类
        public class Tea : Beverage
        {
            public override Size getsize()
            {
                return size;
            }

            public Tea(Size s)
            {
                size = s;
                if (this.getsize() == Size.Big)
                {
                    description = "大Tea";
                }
                if (this.getsize() == Size.Mid)
                {
                    description = "中Tea";
                }
                if (this.getsize() == Size.Small)
                {
                    description = "小Tea";
                }
            }

            public override string getDescription()
            {
                return this.description;
            }

            public override double cost()
            {
               
                return 10;
            }
        }

        //容器大小
        public enum Size
        {
            Big,Small,Mid
        }

        //装饰者类 Decorator,其实也是继承饮料类,可用它也可不用
        public abstract class Decorator : Beverage
        {
            //public abstract new string getDescription();
        }

        //材料:摩卡
        public class Mocha : Beverage
        {
            Beverage bc;

            public override Size getsize()
            {
                return bc.size;
            }

            public Mocha(Beverage b)
            {
                bc = b;
            }

            public override string getDescription()
            {
                return bc.getDescription()  + "[+摩卡]";
            }

            public override double cost()
            {
                double d=bc.cost();
                if (bc.getsize() == Size.Big)
                {
                    d += 0.3;
                }
                if (bc.getsize() == Size.Mid)
                {
                    d += 0.2;
                }
                if (bc.getsize() == Size.Small)
                {
                    d += 0.1;
                }
                return d;
            }
        }

        //材料:牛奶
        public class Milk : Beverage
        {
            Beverage bc;

            public override Size getsize()
            {
                return bc.size;
            }

            public Milk(Beverage b)
            {
                bc = b;
            }

            public override string getDescription()
            {
                return bc.getDescription() + "[+牛奶]";
            }

            public override double cost()
            {
                double d = bc.cost();
                if (bc.getsize() == Size.Big)
                {
                    d += 0.32;
                }
                if (bc.getsize() == Size.Mid)
                {
                    d += 0.22;
                }
                if (bc.getsize() == Size.Small)
                {
                    d += 0.12;
                }
                return d;
            }
        }

        //材料:豆奶
        public class Soy : Beverage
        {
            Beverage bc;

            public override Size getsize()
            {
                return bc.size;
            }

            public Soy(Beverage b)
            {
                bc = b;
            }

            public override  string getDescription()
            {
                return bc.getDescription() + "[+豆奶]";
            }

            public override double cost()
            {
                double d = bc.cost();
                if (bc.getsize() == Size.Big)
                {
                    d += 0.31;
                }
                if (bc.getsize() == Size.Mid)
                {
                    d += 0.21;
                }
                if (bc.getsize() == Size.Small)
                {
                    d += 0.11;
                }
                return d;
            }
        }


    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Decorator
    {
        class Program
        {
            static void Main(string[] args)
            {
                //来一小杯茶
                Beverage be = new Tea(Size.Small);

                System.Console.WriteLine("饮料:" + be.getDescription() + ",价格:¥" + be.cost().ToString() + "人民币");

                //添加牛奶
                be = new Milk(be);
                System.Console.WriteLine(be.GetType().ToString());
                //添加摩卡
                be = new Mocha(be);
                System.Console.WriteLine(be.GetType().ToString());
                System.Console.WriteLine("最后结账:" + be.getDescription() + ",价格:¥" + be.cost().ToString() + "人民币");
               


                //来一大杯咖啡
                Beverage Cof = new Coffe(Size.Mid);

                System.Console.WriteLine("饮料:" + Cof.getDescription() + ",价格:¥" + Cof.cost().ToString() + "人民币");

                //添加豆奶
                Cof = new Soy(Cof);
                System.Console.WriteLine(Cof.GetType().ToString());
                //添加两次摩卡
                Cof = new Mocha(Cof);
                Cof = new Mocha(Cof);
                System.Console.WriteLine(Cof.GetType().ToString());
                System.Console.WriteLine("最后结账:" + Cof.getDescription() + ",价格:¥" + Cof.cost().ToString() + "人民币");
                System.Console.ReadLine();


            }
        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    带宽利用率提升50%,腾讯云联网架构方案解析
    重构实践:基于腾讯云Elasticsearch搭建QQ邮箱全文检索
    存算分离下写性能提升10倍以上,EMR Spark引擎是如何做到的?
    秒级去重:ClickHouse在腾讯海量游戏营销活动分析中的应用
    降本提效,贝壳搜索推荐架构统一之路
    亿级用户,腾讯看点信息流推荐系统的架构挑战
    优秀程序员,如何提高架构能力?
    如何创建体元栅格?
    导出属性表字段结构
    去除镶嵌数据集中影像的黑边或白边
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519218.html
Copyright © 2020-2023  润新知