《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:仅有一个Implementor
2014-08-03 22:00
651 查看
示例:仅有一个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
说明:
在仅有一个实现的时候,没有必要创建一个抽象的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设计模式》—抽象工厂(Abstract Factory)—Delphi源码示例:基于原型的工厂
- 《GOF设计模式》—生成器(Builder)—Delphi源码示例:RTF阅读器
- 《GOF设计模式》—外观(FACADE)—Delphi源码示例:基于外观模式的编译子系统
- 《GOF设计模式》—生成器(Builder)—Delphi源码示例:编译子系统中的ByteCodeStream
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:延迟初始化对象
- 《GOF设计模式》—抽象工厂(Abstract Factory)—Delphi源码示例:用户界面工具包
- 《GOF设计模式》—抽象工厂(Abstract Factory)—Delphi源码示例:抽象工厂接口
- 《GOF设计模式》—创建型模式—Delphi源码示例:未基于模式的迷宫