您的位置:首页 > 编程语言 > Delphi

《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:仅有一个Implementor

2010-11-27 14:18 645 查看
示例:仅有一个Implementor
说明:
在仅有一个实现的时候,没有必要创建一个抽象的Implementor类。这是Bridge模式的退化情况;在Abstraction与Implementor之间有一种一对一的关系。尽管如此,当你希望改变一个类的实现不会影响已有的客户程序时,模式的分离机制还是非常有用的—也就是说,不必重新编译它们,仅需重新连接即可(例如,Abstraction在主程序实现,Implementor在DLL中实现)。

代码:

unit uBridge1;

interface

uses
Dialogs;

type
TImplementor = class;

{抽象类}
TAbstraction = class
private
FImp: TImplementor;
public
constructor Create(AImplementor: TImplementor);
//---
procedure Operation; virtual;
end;
TRefinedAbstraction = class(TAbstraction)
public
procedure Operation; override;
end;

{实现类}
TImplementor = class
procedure OperationImp;
end;

implementation

constructor TAbstraction.Create(AImplementor: TImplementor);
begin
FImp := AImplementor;
end;

procedure TAbstraction.Operation;
begin
FImp.OperationImp;
end;

procedure TImplementor.OperationImp;
begin
ShowMessage('Implementor');
end;

procedure TRefinedAbstraction.Operation;
begin
inherited;
//---
ShowMessage('Refined');
end;

end.

procedure TForm1.Button1Click(Sender: TObject);
var
AImplementor: TImplementor;
AAbstraction: TAbstraction;
begin
AImplementor := TImplementor.Create;
AAbstraction := TRefinedAbstraction.Create(AImplementor);
try
AAbstraction.Operation;
finally
AAbstraction.Free;
AImplementor.Free;
end;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐