用C#改写Head First Design Patterns--Decorator装饰(原创)

 

饮料的包装

 

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();


        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值