《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:装饰接口
来源:互联网 发布:悉尼生活费 知乎 编辑:程序博客网 时间:2024/05/01 04:46
示例:装饰接口
说明:
(1)、定义
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
(2)、结构
组件:
Component:组件。定义一个对象接口,可以给这些对象动态地添加职责。
ConcreteComponent:具体组件。定义一个对象,可以给这个对象添加一些职责。
装饰组件:
Decorator:装饰组件。维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。
ConcreteDecorator:具体装饰组件。向组件添加职责。
协作:
Decorator将请求转发给它的Component对象,并有可能在转发请求前后执行一些附加的动作。
代码: unit uDecortor; interface uses Dialogs; type TComponent = class public procedure Operation; virtual; abstract; end; TConcreteComponent = class(TComponent) public procedure Operation; override; end; TDecorator = class(TComponent) private FComponent: TComponent; public constructor Create(const AComponent: TComponent); destructor Destroy; override; //--- procedure Operation; override; end; TConcreteDecoratorA = class(TDecorator) private FAddedState: string; public constructor Create(const AComponent: TComponent; const AAddedState: string); //--- procedure Operation; override; end; TConcreteDecoratorB = class(TDecorator) private procedure AddedBehavior; public procedure Operation; override; end; implementation constructor TDecorator.Create(const AComponent: TComponent); begin inherited Create; //--- FComponent := AComponent; end; destructor TDecorator.Destroy; begin if Assigned(FComponent) then FComponent.Free; //--- inherited; end; procedure TDecorator.Operation; begin FComponent.Operation; end; procedure TConcreteComponent.Operation; begin ShowMessage('ConcreteComponent'); end; constructor TConcreteDecoratorA.Create(const AComponent: TComponent; const AAddedState: string); begin inherited Create(AComponent); //--- FAddedState := AAddedState; end; procedure TConcreteDecoratorA.Operation; begin inherited; //--- ShowMessage(FAddedState); end; procedure TConcreteDecoratorB.AddedBehavior; begin ShowMessage('AddedBehavior'); end; procedure TConcreteDecoratorB.Operation; begin inherited; //--- AddedBehavior; end; end. procedure TForm1.Button1Click(Sender: TObject); var AComponent: TComponent; begin AComponent := TConcreteDecoratorA.Create(TConcreteDecoratorB.Create(TConcreteComponent.Create), '123'); AComponent.Operation; AComponent.Free; end;
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:装饰接口
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:图形用户界面组件
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:改变对象内核
- GOF设计模式之DECORATOR(装饰)
- Decorator装饰者模式 - GoF设计模式(二)
- GOF 23设计模式之 装饰模式(Decorator)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:适配器接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:桥接接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:最大化Component接口
- 《GOF设计模式》—享元(FLYWEIGHT)—Delphi源码示例:享元接口
- 《GOF设计模式》—代理(PROXY)—Delphi源码示例:代理接口
- 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:命令接口
- 《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:备忘录接口
- 《GOF设计模式》—观察者(OBSERVER)—Delphi源码示例:观察者接口
- 《GOF设计模式》—状态(STATE)—Delphi源码示例:状态接口
- 《GOF设计模式》—访问者(VISITOR)—Delphi源码示例:访问者接口
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:适配器接口
- Memento
- C#中使用GDI+绘制一把漂亮的直尺(带透明度
- 在linux下用ffmpeg摄录摄像头视频
- 32位程序专用文件操作函数
- RAR for Linux 命令详解(`GLIBC_2.7' not found )
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:装饰接口
- servlet与php的xmlrpc
- J2ME数组的复制及连接操作
- Fortran库函数atan与atan2
- WPF/Silverlight动画、游戏教程汇总
- js 打印时如何 选择默认打印机
- 创业有道”公益创业比赛,“婴之都”关爱宝宝新品优惠
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:图形用户界面组件
- HTC G3 Android 2.1 刷机成功