关闭

装饰者模式——HeaderFirst学习笔记

151人阅读 评论(0) 收藏 举报
分类:

1 装饰者模式定义

动态的将责任附加到对象上,想要扩展功能,装饰者提供有别于继承的另外一种选择。

2 装饰者模式精髓

2.1对扩展开放,对修改关闭

2.2继承属于扩展形式之一,但不见得是达到弹性设计的最佳方案

2.3设计应该允许行为被扩展而无需修改现有代码

2.4装饰者意味着一群装饰者类,这些类用来包装具体的组件

2.5装饰者和被装饰者具备同样的类型

2.6装饰者对组件的客户是透明的,除非客户程序依赖组件具体类型

2.7装饰者会产生很多小对象,运用过度会使得程序复杂

3 例子代码

namespace DecoratorDesignPattern
{

    public interface  Deverage
    {

         string getDescription();

         double cost();
    }

    public abstract class ConditionDecorator : Deverage
    {
        public virtual string getDescription() 
        { 
            return string.Empty;
        }

        public virtual double cost()
        {
            return 0.0;
        }
    }

    public class HouseBlend : Deverage
    {

        public HouseBlend(string description)
        {
            m_description = description;
        }

        private string m_description;

        #region Deverage 成员

        public string getDescription()
        {
            return m_description;
        }

        public double cost()
        {
            return 100;
        }

        #endregion
    }

    public class DarkRoast : Deverage
    {

        public DarkRoast(string description)
        {
            m_description = description;
        }

        private string m_description;

        #region Deverage 成员

        public string getDescription()
        {
            return m_description;
        }

        public double cost()
        {
            return 200;
        }

        #endregion
    }

    public class Decaf : Deverage
    {

        public Decaf(string description)
        {
            m_description = description;
        }

        private string m_description;

        #region Deverage 成员

        public string getDescription()
        {
            return "Decaf";
        }

        public double cost()
        {
            return 300;
        }

        #endregion
    }

    public class Espresso : Deverage
    {

        public Espresso(string description)
        {
            m_description = description;
        }

        private string m_description;

        #region Deverage 成员

        public string getDescription()
        {
            return "Espresso";
        }

        public double cost()
        {
            return 400;
        }

        #endregion
    }


    public class Moca : ConditionDecorator
    {
        private Deverage m_Deverage;

        public Moca(Deverage deverage)
        {
            m_Deverage = deverage;
        }

        #region Deverage 成员

        public override string getDescription()
        {
            return m_Deverage.getDescription()+",Moca";
        }

        public override double cost()
        {
            return m_Deverage.cost()+20;
        }

        #endregion
    }

    public class Soy : ConditionDecorator
    {
        private Deverage m_Deverage;

        public Soy(Deverage deverage)
        {
            m_Deverage = deverage;
        }

        #region Deverage 成员

        public override string getDescription()
        {
            return m_Deverage.getDescription() + ",Soy";
        }

        public override double cost()
        {
            return m_Deverage.cost() + 30;
        }

        #endregion
    }

    public class Whip : ConditionDecorator
    {
        private Deverage m_Deverage;

        public Whip(Deverage deverage)
        {
            m_Deverage = deverage;
        }

        #region Deverage 成员

        public override string getDescription()
        {
            return m_Deverage.getDescription() + ",Whip";
        }

        public override double cost()
        {
            return m_Deverage.cost() + 40;
        }

        #endregion
    }
}

namespace DecoratorDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Deverage pCoffee = new HouseBlend("深焙咖啡");

            Deverage pCondiment = new Moca(pCoffee);
            pCondiment = new Soy(pCondiment);
            pCondiment = new Whip(pCondiment);

            Console.WriteLine("咖啡名字为:" + pCondiment.getDescription());
            Console.WriteLine("咖啡价格为:" + pCondiment.cost().ToString());
        }
    }
}


4 设计类图


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6991次
    • 积分:412
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条