我有许多接口和对象。代码看起来像这样:
IInterfaceZ = interface(IInterface)
['{DA003999-ADA2-47ED-A1E0-2572A00B6D75}']
procedure DoSomething;
end;
IInterfaceY = interface(IInterface)
['{55BF8A92-FCE4-447D-B58B-26CD9B344EA7}']
procedure DoNothing;
end;
TObjectB = class(TInterfacedObject, IInterfaceZ)
procedure DoSomething;
end;
TObjectC = class(TInterfacedObject, IInterfaceY)
public
FTest: string;
procedure DoNothing;
end;
TObjectA = class(TInterfacedObject, IInterfaceZ, IInterfaceY)
private
FInterfaceB: IInterfaceZ;
FObjectC: TObjectC;
function GetBB: IInterfaceZ;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property BB: IInterfaceZ read GetBB implements IInterfaceZ;
property CC: TObjectC read FObjectC implements IInterfaceY;
end;
procedure TObjectB.DoSomething;
begin
Sleep(1000);
end;
procedure TObjectA.AfterConstruction;
begin
inherited;
FInterfaceB := TObjectB.Create;
FObjectC := TObjectC.Create;
FObjectC.FTest := 'Testing';
end;
procedure TObjectA.BeforeDestruction;
begin
FreeAndNil(FObjectC);
FInterfaceB := nil;
inherited;
end;
function TObjectA.GetBB: IInterfaceZ;
begin
Result := FInterfaceB;
end;
procedure TObjectC.DoNothing;
begin
ShowMessage(FTest);
end;
现在,如果我像这样访问各种实现,我会得到以下结果:
procedure TestInterfaces;
var
AA: TObjectA;
YY: IInterfaceY;
ZZ: IInterfaceZ;
NewYY: IInterfaceY;
begin
AA := TObjectA.Create;
// Make sure that the Supports doesn't kill the object.
// This line of code is necessary in XE2 but not in XE4
AA._AddRef;
// This will add one to the refcount for AA despite the fact
// that AA has delegated the implementation of IInterfaceY to
// to FObjectC.
Supports(AA, IInterfaceY, YY);
YY.DoNothing;
// This will add one to the refcount for FInterfaceB.
// This is also allowing a supports from a delegated interface
// to another delegated interface.
Supports(YY, IInterfaceZ, ZZ);
ZZ.DoSomething;
// This will fail because the underlying object is actually
// the object referenced by FInterfaceB.
Supports(ZZ, IInterfaceY, NewYY);
NewYY.DoNothing;
end;
第一个Supports调用使用implements中的变量返回YY,实际上是TObjectA的引用。我的AA变量有引用计数。因为底层的引用计数对象是TObjectA,所以第二个supports使用接口在supports调用中可以工作并返回一个接口。底层对象实际上现在是TObjectB。FInterfaceB内部对象是被引用计数的对象。这部分内容是有意义的,因为GetBB实际上是FInterfaceB。最后一个Supports调用返回NewYY的空值,最后的调用失败了。
我的问题是,第一个Supports调用中对TObjectA的引用计数是设计上的吗?换句话说,当实现接口的属性返回一个对象而不是一个接口时,这是否意味着所有者对象将进行引用计数?我一直以为implements也会导致内部委托对象被引用计数,而不是主对象。
声明如下:
property BB: IInterfaceZ read GetBB implements IInterfaceZ;
使用上述选项,FInterfaceB背后的内部对象是被引用计数的那个对象。
property CC: TObjectC read FObjectC implements IInterfaceY;
使用以上第二种选项,被引用计数的是TObjectA而不是委托对象FObjectC。这是设计如此吗?
编辑:我在XE2中进行了测试,行为不同。第二个Supports语句对ZZ返回nil。XE4中的调试器告诉我YY是指(TObjectA as IInterfaceY)。而在XE2中,则告诉我它是(Pointer as IInterfaceY)。此外,在XE2中,第一个支持语句中的AA没有被引用计数,但内部的FObjectC被引用计数。
补充问题回答后的附加信息:
这有一个警告。你可以链接接口版本,但不能链接对象版本。这意味着像这样的东西将会起作用:
TObjectBase = class(TInterfacedObject, IMyInterface)
…
end;
TObjectA = class(TInterfacedObject, IMyInterface)
FMyInterfaceBase: IMyInterface;
property MyDelegate: IMyInterface read GetMyInterface implements IMyInterface;
end;
function TObjectA.GetMyInterface: IMyInterface;
begin
result := FMyInterfaceBase;
end;
TObjectB = class(TInterfacedObject, IMyInterface)
FMyInterfaceA: IMyInterface;
function GetMyInterface2: IMyInterface;
property MyDelegate2: IMyInterface read GetMyInterface2 implements IMyInterface;
end;
function TObjectB.GetMyInterface2: IMyInterface;
begin
result := FMyInterfaceA;
end;
但是使用对象版本会出现编译错误,提示TObjectB没有实现接口的方法。
TObjectBase = class(TInterfacedObject, IMyInterface)
…
end;
TObjectA = class(TInterfacedObject, IMyInterface)
FMyObjectBase: TMyObjectBase;
property MyDelegate: TMyObjectBase read FMyObjectBase implements IMyInterface;
end;
TObjectB = class(TInterfacedObject, IMyInterface)
FMyObjectA: TObjectA;
property MyDelegate2: TObjectA read FMyObjectA implements IMyInterface;
end;
因此,如果您想开始链接委托,那么您需要遵守接口或另找方法解决。
TObjectA
的弱引用,因此您将失去AA以及其余内容。 - Sir Rufo