博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用C#改写Head First Design Patterns--Decorator装饰(原创)
阅读量:4135 次
发布时间:2019-05-25

本文共 3750 字,大约阅读时间需要 12 分钟。

 

饮料的包装

 

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

        }
    }
}

 

转载地址:http://wvpvi.baihongyu.com/

你可能感兴趣的文章
禁止使用类的copy构造函数和赋值操作符
查看>>
C++学习路线
查看>>
私有构造函数
查看>>
组队总结
查看>>
TitledBorder 设置JPanel边框
查看>>
DBCP——开源组件 的使用
查看>>
抓包工具
查看>>
海量数据相似度计算之simhash和海明距离
查看>>
DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
查看>>
DeepLearning tutorial(6)易用的深度学习框架Keras简介
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
流形学习-高维数据的降维与可视化
查看>>
Python-OpenCV人脸检测(代码)
查看>>
python+opencv之视频人脸识别
查看>>
人脸识别(OpenCV+Python)
查看>>
6个强大的AngularJS扩展应用
查看>>
网站用户登录系统设计——jsGen实现版
查看>>
第三方SDK:讯飞语音听写
查看>>
第三方SDK:JPush SDK Eclipse
查看>>
第三方开源库:imageLoader的使用
查看>>