用Delphi实现四人帮(Gof)设计模式-3装饰模式 Decorator MOdel - cococlout的专栏 - CSDN博客
来源:百度文库 编辑:神马文学网 时间:2024/04/29 17:38:25
用Delphi实现四人帮(Gof)设计模式-3装饰模式 Decorator MOdel收藏
装饰模式能够动态地给一个对象添加一些额外的职责,就扩展功能来说,装饰模式比生成子类更加灵活,采用了外挂的方式.参考下面类图中的说明文字,
掌握子类和装饰类的区别是掌握装饰模式的根本:
首先实现业务类TComponent和子类TConcreteComponent,放到BusinessComponents单元中
- {**************************************************
- 装饰模式(Decorator Model)
- 本单元包含一个业务对象父类和一个子类
- UnitOwner:coco.zhang
- Last Modified:2008-10-3
- **************************************************}
- unit BusinessComponents;
- interface
- type
- TComponent = class
- private
- public
- procedure Operation;virtual;abstract;
- end;
- TConcreteComponent = class(TComponent)
- public
- procedure Operation;override;
- procedure AddedOperation;
- end;
- implementation
- procedure TConcreteComponent.Operation;
- begin
- Writeln(' TConcreteComponent.Operation'+ #10+#13 +'----这是父类规定的接口,子类进行了重写');
- end;
- procedure TConcreteComponent.AddedOperation;
- begin
- Writeln(' TConcreteComponent.AddedOperation'+ #10+#13 +'----这是子类新增的方法,和父类无关');
- end;
- initialization
- finalization
- end.
测试业务类Decorator.dpr
- program Decorator;
- {$APPTYPE CONSOLE}
- uses
- BusinessComponents in 'BusinessComponents.pas',
- SysUtils;
- var
- AComponent : TComponent;
- AConcreteComponent : TConcreteComponent;
- begin
- {实例化业务对象,通过多态方式调用}
- AComponent := TConcreteComponent.Create;
- AComponent.Operation;
- {为了调用子类的个性化方法,类型转换}
- AConcreteComponent := TConcreteComponent(AComponent);
- AConcreteComponent.AddedOperation;
- end.
执行结果:
D:\Projects\Delphi7\src\GofProjects\Decorator>Decorator
TConcreteComponent.Operation
----这是父类规定的接口,子类进行了重写
TConcreteComponent.AddedOperation
----这是子类新增的方法,和父类无关
编写包装类
- {**************************************************}
- { }
- {本单元包含一个装饰父类两个具体装饰子类 }
- { }
- { UnitOwner:coco.zhang }
- { Last Modified:2008-10-2 }
- {**************************************************}
- unit DecoratorUnit;
- interface
- uses
- BusinessComponents;
- type
- {包装类基类}
- TComponentDecorator = class(TComponent)
- protected
- BeDecoretedcomponent : TComponent;
- public
- constructor Create(ArgComponent : TComponent );
- procedure Operation;override;
- end;
- {包装类子类}
- TConcreteDecoratorA = class(TComponentDecorator)
- public
- procedure Operation;override;
- procedure DecoratedOperation;
- end;
- implementation
- {实现包装类基类}
- constructor TComponentDecorator.Create(ArgComponent : TComponent );
- begin
- BeDecoretedcomponent := ArgComponent;
- end;
- procedure TComponentDecorator.Operation;
- begin
- if BeDecoretedcomponent <> nil then
- BeDecoretedcomponent.Operation;
- //Writeln('TComponentDecorator Operation方法');
- end;
- {实现包装类子类}
- procedure TConcreteDecoratorA.Operation;
- begin
- Writeln('TConcreteDecoratorA 对象中调用父类 Operation方法');
- inherited Operation;
- DecoratedOperation;
- end;
- procedure TConcreteDecoratorA.DecoratedOperation;
- begin
- Writeln('TConcreteDecoratorA 包装的功能');
- end;
- //initialize
- //finalize
- end.
改写客户端
- program Decorator;
- {$APPTYPE CONSOLE}
- uses
- BusinessComponents in 'BusinessComponents.pas',
- DecoratorUnit,
- SysUtils;
- var
- AComponent : TComponent;
- AConcreteComponent : TConcreteComponent;
- ADecorator:TConcreteDecoratorA;
- begin
- {实例化业务对象,通过多态方式调用}
- AComponent := TConcreteComponent.Create;
- // AComponent.Operation;
- {为了调用子类的个性化方法,类型转换}
- AConcreteComponent := TConcreteComponent(AComponent);
- //AConcreteComponent.AddedOperation;
- ADecorator := TConcreteDecoratorA.Create(AConcreteComponent);
- ADecorator.Operation;
- end.
所谓包装也可以理解为包装类的对象中维护了一个带包装的对象,两者之间是组合关系. 每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链中的.
总之,包装模式提供了为已有功能动态地添加更多功能的一种方式,即不断地把被包装对象的Operation方法强大了,或者说TComponent对象的Operation被修饰了.
这些修饰往往是为了满足某种特殊情况, 不易在主类中全部罗列,而且我们可以提供多种包装方法,以满足特殊需求.
我们应当养成有效地把类的核心职责和装饰功能区分开来的能力和习惯
用Delphi实现四人帮(Gof)设计模式-3装饰模式 Decorator MOdel - cococlout的专栏 - CSDN博客
用Delphi实现四人帮(Gof)设计模式-8构建者模式 Builder - cococlout的专栏 - CSDN博客
用Delphi实现四人帮(Gof)设计模式-9观察者模式Publish/Subscribe,Observer - cococlout的专栏 - CSDN博客
用Delphi实现四人帮(Gof)设计模式-6 模板方法 Template Method - cococlout的专栏 - CSDN博客
GoF设计模式小结- -
GOF设计模式
GOF设计模式
GoF设计模式小结- -
学习GoF设计模式的重要性 设计模式学习 研究
初探 MVP(Model View Presenter) 设计模式 - mc1035的专栏 - CSDNBlog
observer设计模式的java实现 - 小小冰的专栏
软件设计模式(耦合) - LZD_jay的专栏 - CSDN博客
设计模式:Model View Presenter
博客园 - 企业设计模式读书笔记 Domain Model
GoF设计模式之三 Factory Method- -
GoF设计模式之六 Adaptor- -
GoF设计模式之七 Bridge- -
GoF设计模式之八 Composite- -
GoF设计模式之十 Facade- -
GoF设计模式之十一 Flyweight- -
GoF设计模式之十四 Command- -
GoF设计模式之十五 Interpreter- -
GoF设计模式之十六 Iterator- -
GoF设计模式之十七 Mediator- -