用Delphi实现四人帮(Gof)设计模式-3装饰模式 Decorator MOdel - cococlout的专栏 - CSDN博客

来源:百度文库 编辑:神马文学网 时间:2024/04/29 17:38:25

用Delphi实现四人帮(Gof)设计模式-3装饰模式 Decorator MOdel收藏

装饰模式能够动态地给一个对象添加一些额外的职责,就扩展功能来说,装饰模式比生成子类更加灵活,采用了外挂的方式.参考下面类图中的说明文字,

掌握子类和装饰类的区别是掌握装饰模式的根本:

 

首先实现业务类TComponent和子类TConcreteComponent,放到BusinessComponents单元中

  1. {**************************************************
  2.          装饰模式(Decorator Model)              
  3.   本单元包含一个业务对象父类和一个子类             
  4.                                                   
  5.   UnitOwner:coco.zhang                            
  6.   Last Modified:2008-10-3                         
  7. **************************************************}
  8.  unit BusinessComponents;

  9.  interface

  10.  type 
  11.    TComponent = class
  12.      private 
  13.      public 
  14.        procedure  Operation;virtual;abstract;
  15.    end;
  16.   
  17.    TConcreteComponent = class(TComponent)
  18.      public 
  19.        procedure Operation;override;
  20.        procedure AddedOperation;
  21.    end;

  22. implementation

  23.  procedure TConcreteComponent.Operation;
  24.  begin
  25.    Writeln(' TConcreteComponent.Operation'+ #10+#13 +'----这是父类规定的接口,子类进行了重写');
  26.  end;

  27.  procedure TConcreteComponent.AddedOperation;
  28.  begin
  29.    Writeln(' TConcreteComponent.AddedOperation'+ #10+#13 +'----这是子类新增的方法,和父类无关');
  30.  end;

  31. initialization

  32. finalization
  33. end.

测试业务类Decorator.dpr

 

  1. program Decorator;
  2. {$APPTYPE CONSOLE}

  3. uses
  4.   BusinessComponents in 'BusinessComponents.pas',
  5.   SysUtils;

  6. var 
  7.   AComponent : TComponent;
  8.   AConcreteComponent : TConcreteComponent;
  9. begin
  10.  
  11.   {实例化业务对象,通过多态方式调用}
  12.   AComponent := TConcreteComponent.Create;
  13.   AComponent.Operation;
  14.  {为了调用子类的个性化方法,类型转换}
  15.   AConcreteComponent := TConcreteComponent(AComponent);
  16.   AConcreteComponent.AddedOperation;

  17. end.

执行结果:

D:\Projects\Delphi7\src\GofProjects\Decorator>Decorator
TConcreteComponent.Operation
----这是父类规定的接口,子类进行了重写
TConcreteComponent.AddedOperation
----这是子类新增的方法,和父类无关

 

编写包装类

  1. {**************************************************}
  2. {                                                 } 
  3. {本单元包含一个装饰父类两个具体装饰子类           }
  4. {                                                 }
  5. { UnitOwner:coco.zhang                            }
  6. { Last Modified:2008-10-2                         }
  7. {**************************************************}
  8. unit DecoratorUnit;
  9. interface
  10. uses
  11.   BusinessComponents;
  12. type

  13.    {包装类基类}
  14.    TComponentDecorator = class(TComponent)
  15.      protected 
  16.        BeDecoretedcomponent : TComponent;
  17.      public
  18.        constructor Create(ArgComponent : TComponent ); 
  19.        procedure Operation;override;
  20.    end;

  21.    {包装类子类}
  22.    TConcreteDecoratorA = class(TComponentDecorator)
  23.      public
  24.        procedure Operation;override;
  25.        procedure DecoratedOperation;
  26.    end;
  27.     
  28. implementation

  29.  {实现包装类基类}
  30.   constructor TComponentDecorator.Create(ArgComponent : TComponent );
  31.   begin
  32.     BeDecoretedcomponent := ArgComponent;
  33.   end;

  34.   procedure TComponentDecorator.Operation;
  35.   begin
  36.     if BeDecoretedcomponent <> nil then
  37.       BeDecoretedcomponent.Operation;
  38.     //Writeln('TComponentDecorator Operation方法');
  39.   end;

  40.  {实现包装类子类}
  41.   procedure TConcreteDecoratorA.Operation;
  42.   begin
  43.     Writeln('TConcreteDecoratorA 对象中调用父类 Operation方法');
  44.     inherited Operation;
  45.        DecoratedOperation;
  46.   end;

  47.  procedure TConcreteDecoratorA.DecoratedOperation;
  48.  begin
  49.     Writeln('TConcreteDecoratorA 包装的功能');
  50.  end;

  51. //initialize

  52. //finalize

  53. end.

改写客户端

  1. program Decorator;
  2. {$APPTYPE CONSOLE}

  3. uses
  4.   BusinessComponents in 'BusinessComponents.pas',
  5.   DecoratorUnit,
  6.   SysUtils;

  7. var 
  8.   AComponent : TComponent;
  9.   AConcreteComponent : TConcreteComponent;
  10.   ADecorator:TConcreteDecoratorA;
  11. begin
  12.   
  13.   {实例化业务对象,通过多态方式调用}
  14.   AComponent := TConcreteComponent.Create;
  15.  // AComponent.Operation;
  16.  {为了调用子类的个性化方法,类型转换}
  17.   AConcreteComponent := TConcreteComponent(AComponent);
  18.   //AConcreteComponent.AddedOperation;

  19.   ADecorator := TConcreteDecoratorA.Create(AConcreteComponent);
  20.   ADecorator.Operation;
  21. end.

所谓包装也可以理解为包装类的对象中维护了一个带包装的对象,两者之间是组合关系. 每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链中的.

总之,包装模式提供了为已有功能动态地添加更多功能的一种方式,即不断地把被包装对象的Operation方法强大了,或者说TComponent对象的Operation被修饰了.

这些修饰往往是为了满足某种特殊情况, 不易在主类中全部罗列,而且我们可以提供多种包装方法,以满足特殊需求.

我们应当养成有效地把类的核心职责和装饰功能区分开来的能力和习惯