定义:动态地给一个对象增加其他职责,就增加对象的功能来说,装饰模式比生成子类实现更为灵活。
UML图如下:

Code:
- 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象和抽象装饰器。
- 具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。
- 抽象装饰(Decorator)角色:持有一个构件(Component)对象的实例,以用来对它进行装饰,并定义一个与抽象构件接口一致的接口。
- 具体装饰(Concrete Decorator)角色:负责给构件对象"加上"附加的功能。
-
- 结构图代码:
-
- interface Component
- {
- void Operation();
- }
-
- class ConcreteComponent : Component
- {
- public void Operation()
- {
- Console.WriteLine("ConcreteComponent Operation");
- }
- }
-
- abstract class Decorator : Component
- {
-
- protected Component comp;
-
- public Decorator(Component c)
- {
- this.comp = c;
- }
-
- public abstract void Operation();
- }
-
- class ConcreteDecoratorA : Decorator
- {
- private string addedState;
- public ConcreteDecoratorA(Component c) : base(c) { }
- public string AddedState
- {
- set
- {
- addedState = value;
- }
- }
-
- public override void Operation()
- {
- comp.Operation();
- Console.WriteLine("ConcreteDecoratorA Operation {0}",addedState);
- }
- }
-
- class ConcreteDecoratorB : Decorator
- {
- public ConcreteDecoratorB(Component c) : base(c) { }
- public override void Operation()
- {
- comp.Operation();
- Console.WriteLine("ConcreteDecoratorB Operation ");
- }
-
- public void AddedBehavior()
- {
- Console.WriteLine("This is Added Behavior");
- }
- }
- class Client
- {
- public static void Main()
- {
-
- ConcreteComponent cc = new ConcreteComponent();
- cc.Operation();
-
- ConcreteDecoratorA cda = new ConcreteDecoratorA(cc);
- cda.AddedState = "Decorator OK ";
- cda.Operation();
-
- ConcreteDecoratorB cdb = new ConcreteDecoratorB(cc);
- cdb.AddedBehavior();
- cdb.Operation();
-
- ConcreteDecoratorB cdbcda = new ConcreteDecoratorB(cda);
- cdbcda.Operation();
- }
- }
- Decorator与Component之间既是Is a...的继承关系,又是Has a...的组合关系。使用具体装饰类(ConcreteDecorator)来装饰具体构件对象(ConcreteComponent),装饰完的对象依旧是个Component类型。
Decorator模式解决的是类的功能的多向扩展,而不是单纯的类的继承。
Decorator模式提供了比继承更灵活的功能扩展,通过使用不同具体装饰对构件对象的排列组合地包装,能够不同功能的组合。

- 大小: 432.9 KB