《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(缺省的实现)
2010-11-27 14:21
701 查看
示例:创建正确的Implementor对象(缺省的实现)
说明:
创建正确Implementor对象的另外一种方法是首先选择一个缺省的实现,然后根据需要改变这个实现。
例如,如果一个collection的大小超出了一定的阈值时,它将会切换它的实现,使之更适用于表目较多的collection。
代码:
unit uBridge3;
interface
uses
Dialogs;
type
TTable = class;
{抽象类}
TCollection = class
private
FImp: TTable;
FCount: integer;
procedure SetCount(const Value: integer);
public
constructor Create;
destructor Destroy; override;
//---
procedure Operation; virtual;
property Count: integer read FCount write SetCount;
end;
TCollectionA = class(TCollection)
public
procedure Operation; override;
end;
{实现类}
TTable = class
procedure OperationImp; virtual; abstract;
end;
THash = class(TTable)
procedure OperationImp; override;
end;
TLink = class(TTable)
procedure OperationImp; override;
end;
implementation
constructor TCollection.Create;
begin
FImp := TLink.Create;
end;
destructor TCollection.Destroy;
begin
FImp.Free;
//---
inherited;
end;
procedure TCollection.Operation;
begin
FImp.OperationImp;
end;
procedure TCollection.SetCount(const Value: integer);
begin
if FCount <> Value then
begin
FCount := Value;
//---
FImp.Free;
if FCount < 100 then
FImp := TLink.Create
else
FImp := THash.Create;
end;
end;
procedure THash.OperationImp;
begin
ShowMessage('Hash');
end;
procedure TLink.OperationImp;
begin
ShowMessage('Link');
end;
procedure TCollectionA.Operation;
begin
inherited;
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
ACollection: TCollection;
begin
ACollection := TCollectionA.Create;
try
ACollection.Operation;
//---
ACollection.Count := 200;
ACollection.Operation;
finally
ACollection.Free;
end;
end;
说明:
创建正确Implementor对象的另外一种方法是首先选择一个缺省的实现,然后根据需要改变这个实现。
例如,如果一个collection的大小超出了一定的阈值时,它将会切换它的实现,使之更适用于表目较多的collection。
代码:
unit uBridge3;
interface
uses
Dialogs;
type
TTable = class;
{抽象类}
TCollection = class
private
FImp: TTable;
FCount: integer;
procedure SetCount(const Value: integer);
public
constructor Create;
destructor Destroy; override;
//---
procedure Operation; virtual;
property Count: integer read FCount write SetCount;
end;
TCollectionA = class(TCollection)
public
procedure Operation; override;
end;
{实现类}
TTable = class
procedure OperationImp; virtual; abstract;
end;
THash = class(TTable)
procedure OperationImp; override;
end;
TLink = class(TTable)
procedure OperationImp; override;
end;
implementation
constructor TCollection.Create;
begin
FImp := TLink.Create;
end;
destructor TCollection.Destroy;
begin
FImp.Free;
//---
inherited;
end;
procedure TCollection.Operation;
begin
FImp.OperationImp;
end;
procedure TCollection.SetCount(const Value: integer);
begin
if FCount <> Value then
begin
FCount := Value;
//---
FImp.Free;
if FCount < 100 then
FImp := TLink.Create
else
FImp := THash.Create;
end;
end;
procedure THash.OperationImp;
begin
ShowMessage('Hash');
end;
procedure TLink.OperationImp;
begin
ShowMessage('Link');
end;
procedure TCollectionA.Operation;
begin
inherited;
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
ACollection: TCollection;
begin
ACollection := TCollectionA.Create;
try
ACollection.Operation;
//---
ACollection.Count := 200;
ACollection.Operation;
finally
ACollection.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源码示例:可移植的用户界面
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:桥接接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:共享Implementor对象
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TList和TObject对象服务实现)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:共享Implementor对象
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:避免限定于一种特定的列表实现(一般迭代)
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:改变对象内核
- 《GOF设计模式》—代理(PROXY)—Delphi源码示例:文档编辑器(使用虚代理实现)
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:避免限定于一种特定的列表实现(多态迭代)
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:延迟初始化对象
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用代理对象)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TList实现)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TStringList实现)