定义:装饰器设计模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。不改变对象的情况下,动态增加其功能。
• 需要扩展一个类的功能,或给一个类添加附加的成员。
• 需要动态地给一个对象增加功能,这些功能可以再次动态的撤销
• 需要增加由一些基本功能的排列组合二产生的非常大量的功能,从而使继承关系实现变得不现实。
装饰器模式中的各个角色:
• 抽象构件角色:给出抽象接口。规范被附加功能类型。
• 具体构件角色:被附加功能的具体实现。
• 装饰角色:持有被附加对象的实例,并实现抽象构件的接口规范(装饰角色实现抽象构件的目的就是为了具体装饰角色之间也可以相互修饰)。
• 具体装饰角色:扩展具体构件的功能
客户端可以显式扩展具体构件的功能,由于装饰器继承了抽象构件的,所以装饰器之间可以相互修饰,任意组合,从而扩展出具有排列组合性质的组合。
代码:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 抽象构件
/// </summary>
/// <remarks>给出一个抽象接口,规范被附加方法与属性的接受对象</remarks>
public abstract class Component
{
abstract public void Operation();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体构件
/// </summary>
/// <remarks>Component的实现</remarks>
public class ConcreateComponent : Component
{
public override void Operation()
{
Console.WriteLine("Concreate Component");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体装饰器A
/// </summary>
/// <remarks>给Component添加属性AddState</remarks>
public class ConcreateDecoratorA : Decorator
{
private int addState;
public ConcreateDecoratorA(Component component):base(component)
{
}
/// <summary>
/// 附加的属性
/// </summary>
public int AddProperty
{
get
{
return addState;
}
set
{
addState = value;
}
}
public override void Operation()
{
base.Operation();
Console.WriteLine("AddProperty:{0}", AddProperty);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体装饰器B
/// </summary>
/// <remarks>给Component添加属性AddMethod</remarks>
public class ConcreateDecoratorB : Decorator
{
public ConcreateDecoratorB(Component component):base(component)
{
}
/// <summary>
/// 附加的方法
/// </summary>
public void AddMethod()
{
Console.WriteLine("AddMethod");
}
public override void Operation()
{
base.Operation();
AddMethod();
}
}
}
调用
Component myComponent = new ConcreateComponent();
//给myComponent添加A功能
ConcreateDecoratorA decoratorA = new ConcreateDecoratorA(myComponent);
//给myComponent添加B功能
ConcreateDecoratorB decoratorB = new ConcreateDecoratorB(myComponent);
decoratorA.AddProperty = 100;
Console.WriteLine("给myComponent添加 A属性");
//测试装饰器A
decoratorA.Operation();
Console.WriteLine("给myComponent添加 B方法");
//测试装饰器B
decoratorB.Operation();
//给myComponent添加A,B功能
Console.WriteLine("给myComponent A属性 ,添加 B方法");
decoratorB = new ConcreateDecoratorB(decoratorA);
decoratorB.Operation();
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 抽象构件
/// </summary>
/// <remarks>给出一个抽象接口,规范被附加方法与属性的接受对象</remarks>
public abstract class Component
{
abstract public void Operation();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体构件
/// </summary>
/// <remarks>Component的实现</remarks>
public class ConcreateComponent : Component
{
public override void Operation()
{
Console.WriteLine("Concreate Component");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体装饰器A
/// </summary>
/// <remarks>给Component添加属性AddState</remarks>
public class ConcreateDecoratorA : Decorator
{
private int addState;
public ConcreateDecoratorA(Component component):base(component)
{
}
/// <summary>
/// 附加的属性
/// </summary>
public int AddProperty
{
get
{
return addState;
}
set
{
addState = value;
}
}
public override void Operation()
{
base.Operation();
Console.WriteLine("AddProperty:{0}", AddProperty);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体装饰器B
/// </summary>
/// <remarks>给Component添加属性AddMethod</remarks>
public class ConcreateDecoratorB : Decorator
{
public ConcreateDecoratorB(Component component):base(component)
{
}
/// <summary>
/// 附加的方法
/// </summary>
public void AddMethod()
{
Console.WriteLine("AddMethod");
}
public override void Operation()
{
base.Operation();
AddMethod();
}
}
}
调用
Component myComponent = new ConcreateComponent();
//给myComponent添加A功能
ConcreateDecoratorA decoratorA = new ConcreateDecoratorA(myComponent);
//给myComponent添加B功能
ConcreateDecoratorB decoratorB = new ConcreateDecoratorB(myComponent);
decoratorA.AddProperty = 100;
Console.WriteLine("给myComponent添加 A属性");
//测试装饰器A
decoratorA.Operation();
Console.WriteLine("给myComponent添加 B方法");
//测试装饰器B
decoratorB.Operation();
//给myComponent添加A,B功能
Console.WriteLine("给myComponent A属性 ,添加 B方法");
decoratorB = new ConcreateDecoratorB(decoratorA);
decoratorB.Operation();
输出结果: