zoukankan      html  css  js  c++  java
  • 用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
  • 相关阅读:
    02 : python 基础语法,流程控制语句
    10 : mysql 主从复制
    docker Dockerfile 参数讲解
    docker service 创建swarm节点服务
    docker image 删除未使用的镜像
    docker config 创建配置文件
    docker wait 命令使用
    docker update 更新容器信息
    docker top 命令使用
    docker tag 修改镜像的标枪
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519218.html
Copyright © 2011-2022 走看看