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


        }
    }
}

 

posted @ 2009-07-08 16:03  昕友软件开发  阅读(191)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯