《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;
说明:
在仅有一个实现的时候,没有必要创建一个抽象的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;
相关文章推荐
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:仅有一个Implementor
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(参数)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(缺省的实现)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(参数)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(缺省的实现)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:桥接接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:可移植的用户界面
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:共享Implementor对象
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:可移植的用户界面
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:桥接接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:共享Implementor对象
- 《GOF设计模式》—备忘录(MEMENTO)—Delphi源码示例:一个反映备忘录模式的迭代接口
- 《GOF设计模式》—观察者(OBSERVER)—Delphi源码示例:拉模型(pull model)
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:表示请求(使用处理函数)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:适配器接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:绘图编辑器
- 《GOF设计模式》—模板方法(TEMPLATE METHOD)—Delphi源码示例:文档
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:工厂方法接口
- 《GOF设计模式》—生成器(Builder)—Delphi源码示例:生成器接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:使用高速缓冲存贮改善性能