Delphi 泛型 接口约束的实例 转
通过接受一个限定的参数,这个参数是实现某个接口的类,比较起直接接受泛型,而限制这个泛型的类要更加灵活。也就是通常所说的面向接口式的编程。这样可以 达到调用实现了这个接口的各种泛型的实例。这种对泛型使用接口约束的应用,在.net框架中有很广泛的应用。下面是一个实例(命名为 IntfConstraint)。
[delphi] view plaincopy
-
type
-
IGetValue = interface -
['{60700EC4-2CDA-4CD1-A1A2-07973D9D2444}'] -
function GetValue: Integer; -
procedure SetValue (Value: Integer); -
property Value: Integer -
read GetValue write SetValue; -
end;
[delphi] view plaincopy
-
TGetValue
= class (TSingletonImplementation , IGetValue) -
private -
fValue: Integer; -
public -
constructor Create (Value: Integer); // = 0); -
function GetValue: Integer; -
procedure SetValue (Value: Integer); -
end;
[delphi] view plaincopy
-
TInftClass
= class -
private
-
val1, val2: T; // or IGetValue -
public
-
procedure Set1 (val: T); -
procedure Set2 (val: T); -
function GetMin: Integer; -
function GetAverage: Integer; -
procedure IncreaseByTen; -
end;
[delphi] view plaincopy
-
function
TInftClass.GetMin: Integer; -
begin
-
if Assigned (val1) and Assigned (val2) then -
Result := min (val1.GetValue, val2.GetValue) -
else -
Result := 0; -
end;
-
-
procedure
TInftClass.IncreaseByTen; -
begin
-
if Assigned (val1) and Assigned (val2) then -
begin -
val1.SetValue (val1.GetValue + 10); -
val2.SetValue (val2.GetValue + 10); -
end; -
end;
[delphi] view plaincopy
-
procedure
TFormIntfConstraint.btnValueClick(Sender: TObject); -
var
-
iClass: TInftClass; -
begin
-
iClass := TInftClass.Create; -
try -
iClass.Set1 (TGetValue.Create (5)); -
iClass.Set2 (TGetValue.Create (25)); -
Log ('Average: ' + IntToStr (iClass.GetAverage)); -
iClass.IncreaseByTen; -
Log ('Min: ' + IntToStr (iClass.GetMin)); -
finally -
iClass.val1.Free; -
iClass.val2.Free; -
iCLass.Free; -
end; -
end;
[delphi] view plaincopy
-
//
IGetValue 的第二种实现, 对TButton对象 -
TButtonValue
= class (TButton, IGetValue) -
public
-
function GetValue: Integer; -
procedure SetValue (Value: Integer); -
class function MakeTButtonValue ( -
Owner: TComponent; Parent: TWinControl): TButtonValue; -
end;
[delphi] view plaincopy
-
//
ButtonValue 成员函数,用来生成按钮的坐标位置 -
class
function TButtonValue.MakeTButtonValue(Owner: TComponent; -
Parent: TWinControl): TButtonValue; -
begin
-
Result := TButtonValue.Create(Owner); -
Result.Parent := Parent; -
Result.SetBounds( -
Random (Parent.Width), Random (Parent.Height), -
Result.Width, Result.Height); -
Result.Caption := 'btnv'; -
end;
[delphi] view plaincopy
-
//
ButtonValue 中实现GetValue和SetValue -
function
TButtonValue.GetValue: Integer; -
begin
-
Result := Left; -
end;
-
-
procedure
TButtonValue.SetValue(Value: Integer); -
begin
-
Left := Value; -
end;
[delphi] view plaincopy
-
//
主窗体中测试第二种(按钮的位置)泛型调用 -
procedure
TFormIntfConstraint.btnValueButtonClick(Sender: TObject); -
var
-
iClass: TInftClass; -
begin
-
iClass := TInftClass.Create; -
try -
iClass.Set1 (TButtonValue.MakeTButtonValue (self, ScrollBox1)); -
iClass.Set2 (TButtonValue.MakeTButtonValue (self, ScrollBox1)); -
Log ('Average: ' + IntToStr (iClass.GetAverage)); -
Log ('Min: ' + IntToStr (iClass.GetMin)); -
iClass.IncreaseByTen; -
Log ('New Average: ' + IntToStr (iClass.GetAverage)); -
finally -
iClass.Free; -
end; -
end;