设计模式 之 装饰者

装饰模式(Deocrator)
          动态地给一个对象加入一些额外的职责。就添加功能来说,装饰模式比生成子类更为灵活。






        所谓装饰,就是一些对象给主题对象做陪衬。

我们能够想象,在一个公司里面。每一个人都有一个办工作,办工作都须要有电脑、电话、目录、盆栽、签字笔、公章等作为装饰。可是不同的人的办公桌上的装饰肯定不一样。比方说,老总的办公桌上应该什么都有,可是一般工作人员的办公桌上,就不应该有电话和公章。

我们怎么动态的来装饰办公桌呢?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 装饰
{
    class Program
    {
        //办公桌
        public class officeDesk
        {
            public officeDesk()
            { }
            private string userName;
            public officeDesk(string userName)
            {
                this.userName = userName;
            }
            public virtual void show()
            {
                Console.WriteLine("{0}的办公桌:",userName );
            }
        }
        //装饰者
        public class Decorator : officeDesk
        {
            //须要知道装饰对象。相应UML中的聚合
            protected officeDesk desk;
            //装扮办公桌
            public void setDecortate(officeDesk desk)
            {
                this.desk = desk;
            }

            public override void show()
            {
                if (desk!=null)
                {
                    desk.show();
                }
            }

        }

        //详细装饰类
        //电脑
        public class computer:Decorator
        {
            public override void show()
            {
                base.show();
                Console.WriteLine("一台电脑 ");
            }
        }
        //电话
        public class telephone : Decorator
        {
            public override void show()
            {
                base.show();
                Console.WriteLine("一部电话 ");
            }
        }
        //目录
        public class file : Decorator
        {
            public override void show()
            {
                base.show();
                Console.WriteLine("一个目录 ");
            }
        }
        //盆栽
        public class plant : Decorator
        {
            public override void show()
            {
                base.show();
                Console.WriteLine("一盆盆栽 ");
            }
        }
        //签字笔
        public class pen : Decorator
        {
            public override void show()
            {
                base.show();
                Console.WriteLine("一支签字笔 ");
            }
        }
        //公章
        public class seal : Decorator
        {
            public override void show()
            {
                base.show();
                Console.WriteLine("一个公章 ");
            }
        }


        static void Main(string[] args)
        {
            officeDesk renZong = new officeDesk("任总");

            computer computer = new computer();
            telephone telephone = new telephone();
            file file = new file();
            plant plant = new plant();
            pen pen = new pen();
            seal seal = new seal();

            computer.setDecortate(renZong);
            telephone.setDecortate(computer);
            file.setDecortate(telephone);
            plant.setDecortate(file);
            pen.setDecortate(plant );
            seal.setDecortate(pen);
            seal.show();

            Console.WriteLine();
            officeDesk xiaoLi = new officeDesk("小李");

            computer.setDecortate(xiaoLi);
            file.setDecortate(computer);
            pen.setDecortate(file );
            pen.show();

            Console.Read();
        }
    }
}





装饰者类图:

                                                 



在装饰模式结构图中包括例如以下几个角色:
       Component(抽象构件):它是详细构件和抽象装饰类的共同父类,声明了在详细构件中实现的业务方法,它的引入能够使client以一致的方式处理未被装饰的对象以及装饰之后的对象,实现client的透明操作。


       ConcreteComponent(详细构件):它是抽象构件类的子类,用于定义详细的构件对象,实现了在抽象构件中声明的方法,装饰器能够给它添加额外的职责(方法)。
       Decorator(抽象装饰类):它也是抽象构件类的子类,用于给详细构件添加职责,可是详细职责在其子类中实现。它维护一个指向抽象构件对象的引用,通过该引用能够调用装饰之前构件对象的方法。并通过其子类扩展该方法。以达到装饰的目的。
       ConcreteDecorator(详细装饰类):它是抽象装饰类的子类。负责向构件加入新的职责。

每个详细装饰类都定义了一些新的行为,它能够调用在抽象装饰类中定义的方法。并能够添加新的方法用以扩充对象的行为。




主要长处:
       1.对于扩展一个对象的功能,装饰模式比继承更加灵活性,不会导致类的个数急剧添加。
       2.能够通过一种动态的方式来扩展一个对象的功能,通过配置文件能够在执行时选择不同的详细装饰类,从而实现不同的行为。
       3.能够对一个对象进行多次装饰。通过使用不同的详细装饰类以及这些装饰类的排列组合。能够创造出非常多不同行为的组合。得到功能更为强大的对象。
       4.详细构件类与详细装饰类能够独立变化,用户能够依据须要添加新的详细构件类和详细装饰类。原有类库代码无须改变。符合“开闭原则”。




主要缺点:
       1.使用装饰模式进行系统设计时将产生非常多小对象。
       2. 装饰模式提供了一种比继承更加灵活机动的解决方式。但同一时候也意味着比继承更加易于出错,排错也非常困难,对于多次装饰的对象,调试时寻找错误可能须要逐级排查。较为繁琐。


适用场景:
       1.当我们须要为某个现有的对象动态地添加一个新的功能或职责时,能够考虑使用装饰者。
       2. 当不能採用继承的方式对系统进行扩展或者採用继承不利于系统扩展和维护时能够使用装饰模式。


与适配器、策略的差别:

       装饰者是在不改变原内容的基础上,动态地添加新的行为;

       适配器则主要用来填补两个接口之间的差异,将一个接口转化为还有一个接口;

       策略是以切换运算法则的方式变换功能。









posted on 2017-07-05 17:08  ljbguanli  阅读(170)  评论(0)    收藏  举报