Delphi泛型应用示例之一:接口化指针
2015-02-27 10:47
218 查看
接口的引用计数特性可以让我们方便地创建一个接口实例,它可以到处使用而无需手动释放内存。但是假如我们这个接口里面有很多数据项那就麻烦多了,得针对每个数据项分别写出其Get和Set方法,接口要写一遍,类还要写一遍。
举个例子,我们想把这个记录类型用接口的方式实现:
TRecord_Test = record
Data1: string;
Data2: Integer;
Data3: Double;
end;
通常的方法是这样的:
type
TInterface_Test = interface
function GetData1: string;
function GetData2: Integer;
function GetData3: Double;
procedure SetData1(const Value: string);
procedure SetData2(const Value: Integer);
procedure SetData3(const Value: Double);
property Data1: string read GetData1 write SetData1;
property Data2: Integer read GetData2 write SetData2;
property Data3: Double read GetData3 write SetData3;
end;
TClass_Test = class(TInterfacedObject, TInterface_Test)
private
FData2: Integer;
FData3: Double;
FData1: string;
function GetData1: string;
function GetData2: Integer;
function GetData3: Double;
procedure SetData1(const Value: string);
procedure SetData2(const Value: Integer);
procedure SetData3(const Value: Double);
public
property Data1: string read GetData1 write SetData1;
property Data2: Integer read GetData2 write SetData2;
property Data3: Double read GetData3 write SetData3;
end;
implementation
{ TClass_Test }
function TClass_Test.GetData1: string;
begin
Result := FData1;
end;
function TClass_Test.GetData2: Integer;
begin
Result := FData2;
end;
function TClass_Test.GetData3: Double;
begin
Result := FData3;
end;
procedure TClass_Test.SetData1(const Value: string);
begin
FData1 := Value;
end;
procedure TClass_Test.SetData2(const Value: Integer);
begin
FData2 := Value;
end;
procedure TClass_Test.SetData3(const Value: Double);
begin
FData3 := Value;
end;
上面的代码是不是看得人吐血?要是有多个类似的记录,恐怕大家宁愿到处维护指针了。
不要紧,我们有万能的泛型。请看下述代码:
unit BambooInterfacedPointer;
interface
type
TValuePointer<T> = packed record
PValue: ^T;
end;
IBambooInterfacedPointer<T> = interface
function Instance: TValuePointer<T>;
end;
TBambooInterfacedPointer<T> = class(TInterfacedObject, IBambooInterfacedPointer<T>)
strict private
FValuePointer: ^T;
private
function Instance: TValuePointer<T>; inline;
public
constructor Create;
destructor Destroy; override;
end;
implementation
{ TBambooInterfacedPointer<T> }
constructor TBambooInterfacedPointer<T>.Create;
begin
inherited Create;
New(FValuePointer);
System.FillChar(FValuePointer^, SizeOf(T), 0);
end;
destructor TBambooInterfacedPointer<T>.Destroy;
begin
Dispose(FValuePointer);
inherited Destroy;
end;
function TBambooInterfacedPointer<T>.Instance: TValuePointer<T>;
begin
Result.PValue := FValuePointer;
end;
end.
说明一下,之所以要定义
type
TValuePointer<T> = packed record
PValue: ^T;
end;
这么一个东西,是因为在泛型接口里面不允许使用如下写法
IBambooInterfacedPointer<T> = interface
function Instance: ^T;
end;
所以定义了一个记录类型来作为过渡。
最后,则是我写的这个接口化指针的应用实例:
type
TRecord_Test = record
Data1: string;
Data2: Integer;
Data3: Double;
end;
var
aInterfacedPointer: IBambooInterfacedPointer<TRecord_Test>;
begin
aInterfacedPointer := TBambooInterfacedPointer<TRecord_Test>.Create;
with aInterfacedPointer.Instance.PValue^ do
begin
Data1 := '123';
Data2 := 456;
Data3 := 7.89;
end;
end;
是不是很方便啊 ? 重要的是 , 所有的数据类型都可以这么干 !
接口的引用计数特性可以让我们方便地创建一个接口实例,它可以到处使用而无需手动释放内存。但是假如我们这个接口里面有很多数据项那就麻烦多了,得针对每个数据项分别写出其Get和Set方法,接口要写一遍,类还要写一遍。
举个例子,我们想把这个记录类型用接口的方式实现:
TRecord_Test = record
Data1: string;
Data2: Integer;
Data3: Double;
end;
通常的方法是这样的:
type
TInterface_Test = interface
function GetData1: string;
function GetData2: Integer;
function GetData3: Double;
procedure SetData1(const Value: string);
procedure SetData2(const Value: Integer);
procedure SetData3(const Value: Double);
property Data1: string read GetData1 write SetData1;
property Data2: Integer read GetData2 write SetData2;
property Data3: Double read GetData3 write SetData3;
end;
TClass_Test = class(TInterfacedObject, TInterface_Test)
private
FData2: Integer;
FData3: Double;
FData1: string;
function GetData1: string;
function GetData2: Integer;
function GetData3: Double;
procedure SetData1(const Value: string);
procedure SetData2(const Value: Integer);
procedure SetData3(const Value: Double);
public
property Data1: string read GetData1 write SetData1;
property Data2: Integer read GetData2 write SetData2;
property Data3: Double read GetData3 write SetData3;
end;
implementation
{ TClass_Test }
function TClass_Test.GetData1: string;
begin
Result := FData1;
end;
function TClass_Test.GetData2: Integer;
begin
Result := FData2;
end;
function TClass_Test.GetData3: Double;
begin
Result := FData3;
end;
procedure TClass_Test.SetData1(const Value: string);
begin
FData1 := Value;
end;
procedure TClass_Test.SetData2(const Value: Integer);
begin
FData2 := Value;
end;
procedure TClass_Test.SetData3(const Value: Double);
begin
FData3 := Value;
end;
上面的代码是不是看得人吐血?要是有多个类似的记录,恐怕大家宁愿到处维护指针了。
不要紧,我们有万能的泛型。请看下述代码:
unit BambooInterfacedPointer;
interface
type
TValuePointer<T> = packed record
PValue: ^T;
end;
IBambooInterfacedPointer<T> = interface
function Instance: TValuePointer<T>;
end;
TBambooInterfacedPointer<T> = class(TInterfacedObject, IBambooInterfacedPointer<T>)
strict private
FValuePointer: ^T;
private
function Instance: TValuePointer<T>; inline;
public
constructor Create;
destructor Destroy; override;
end;
implementation
{ TBambooInterfacedPointer<T> }
constructor TBambooInterfacedPointer<T>.Create;
begin
inherited Create;
New(FValuePointer);
System.FillChar(FValuePointer^, SizeOf(T), 0);
end;
destructor TBambooInterfacedPointer<T>.Destroy;
begin
Dispose(FValuePointer);
inherited Destroy;
end;
function TBambooInterfacedPointer<T>.Instance: TValuePointer<T>;
begin
Result.PValue := FValuePointer;
end;
end.
说明一下,之所以要定义
type
TValuePointer<T> = packed record
PValue: ^T;
end;
这么一个东西,是因为在泛型接口里面不允许使用如下写法
IBambooInterfacedPointer<T> = interface
function Instance: ^T;
end;
所以定义了一个记录类型来作为过渡。
最后,则是我写的这个接口化指针的应用实例:
type
TRecord_Test = record
Data1: string;
Data2: Integer;
Data3: Double;
end;
var
aInterfacedPointer: IBambooInterfacedPointer<TRecord_Test>;
begin
aInterfacedPointer := TBambooInterfacedPointer<TRecord_Test>.Create;
with aInterfacedPointer.Instance.PValue^ do
begin
Data1 := '123';
Data2 := 456;
Data3 := 7.89;
end;
end;
是不是很方便啊 ? 重要的是 , 所有的数据类型都可以这么干 !
相关文章推荐
- Delphi2009(Tiburon)新特性-泛型示例
- Delphi:记录类型、过程指针、函数(方法)指针字段的应用
- C语言泛型指针应用-为任何类型的变量交换值
- delphi.指针.应用
- 字符串指针在 Delphi 中的应用
- HreoWinGauge2.0组件集应用示例----旋钮指针(1)
- Delphi中GUID相等检查中经典指针应用
- HreoWinGauge2.0组件集应用示例----旋钮指针(2)
- Delphi下WebBrowser应用示例
- Delphi万能指针应用
- 电力系统 接线图,配电图,地理接线图,电力系统组态与仿真,电力调度,自动控制,VC++和DELPHI以及web应用示例与源代码
- 电力系统 接线图,配电图,地理接线图,电力系统组态与仿真,电力调度,自动控制,VC++和DELPHI以及web应用示例与源代码
- 《GOF设计模式》—中介者 (MEDIATOR)—Delphi源码示例:Smalltalk/V的应用结构
- 一个泛型应用示例
- Delphi 泛型应用
- delphi.指针.应用----应用重要 多看 多练
- Delphi下WebBrowser应用示例
- C# delegate , Unity3D 如何应用指针
- Delphi中资源的简单应用
- PCA、SVD应用示例:低维投影可视化