设计模式之装饰者模式

装饰者模式

Decorator模式(别名Wrapper模式),是开放封闭原则的体现(即:你应该能够不用修改原有类就能扩展一个类的行为)

装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

装饰模式通过创建一个包装对象,也就是装饰,来包裹真实的对象。

装饰模式以对客户端透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。

装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展。

装饰模式把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展是完全透明的。




装饰模式的角色

  抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。

  具体构件角色(Concrete Component):定义将要接收附加责任的类。

  装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口。

  具体装饰角色(Concrete Decorator):负责给构件对象“贴上”附加的责任。

装饰模式的特点

  装饰对象和真实对象有相同的接口。这样客户端对象就可以以和真实对象相同的方式和装饰对象交互。

  装饰对象包含一个真实对象的引用(reference)。

  装饰对象接收所有来自客户端的请求,它把这些请求转发给真实的对象。

  装饰对象可以在转发这些请求之前或之后附加一些功能。

  这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。

UML图示

      


程序实例

   

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

//装饰模式又名包装(Wrapper)模式。
namespace Decorator
{
    /// <summary>Component 抽象构件
    /// 这是抽象构件角色,是一个接口
    /// </summary>
    abstract class Component
    {
        // Methods
        public abstract void DoSomething();
    }

    /// <summary>ConcreteComponent 具体构件
    /// 具体构件角色实现这个接口
    /// </summary>
    class ConcreteComponent : Component
    {
        // Methods
        public override void DoSomething()
        {
            Console.WriteLine("主要功能");
        }
    }

    /// <summary>Decorator
    /// 装饰角色 :其中包含了构件角色的引用,方法调用中利用构件角色的方法。
    /// </summary>
    abstract class Decorator : Component
    {
        // Fields
        private Component component;

        // Methods
        protected Decorator(Component component)
        {
            this.component = component;
        }

        public override void DoSomething()
        {
            if (component != null)
                component.DoSomething();
        }
    }

    /// <summary>
    /// 具体装饰角色A
    /// </summary>
    class ConcreteDecoratorA : Decorator
    {
        public ConcreteDecoratorA(Component component)
            : base(component)
        {

        }

        public override void DoSomething()
        {
            base.DoSomething();
            DoSomethingA();
        }
        private void DoSomethingA()
        {
            Console.WriteLine("扩展功能A");
        }
    }
    /// <summary>
    /// 具体装饰角色B
    /// </summary>
    class ConcreteDecoratorB : Decorator
    {
         public ConcreteDecoratorB(Component component)
            : base(component)
        {

        }
        public override void DoSomething()
        {
            base.DoSomething();
            DoSomethingB();
        }
        private void DoSomethingB()
        {
            Console.WriteLine("扩展功能B");
        }
    }

}

客户端调用代码:

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

namespace Decorator
{
    //装饰(Decorator)模式又名包装(Wrapper)模式[GOF95]。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
    class Program
    {
        static void Main(string[] args)
        {
            Component component = new ConcreteComponent();

            Component component1 = new ConcreteDecoratorA(component);

            component1.DoSomething();
            Console.WriteLine("---------------");

            Component component2 = new ConcreteDecoratorB(component1);

            component2.DoSomething();
            Console.ReadLine();
        }
    }
}

输出:

主要功能
扩展功能A
--------------
主要功能
扩展功能A
扩展功能B

参考:

1.http://www.cnblogs.com/god_bless_you/archive/2010/06/10/1755212.html

2.http://baike.baidu.com/view/2787758.htm

原文地址:https://www.cnblogs.com/guanjie20/p/3666409.html