我正在尝试执行 GoF 适配器模式,在 C# 示例中,我遵循的 Adapter 类继承了原始类和一个适配接口。
据我所知,在Delphi(2007)中,这是不可能的,或者是吗?因为如果一个类继承一个接口,它需要从 TInterfacedObject 继承,并且由于 Delphi 不允许多个类继承,这就是故事的结尾。我无法同时继承自定义类和接口。
我对么?
谢谢。
我已经实现了这个模式http://delphipatterns.blog.com/2011/02/22/decorator-5/ http://delphipatterns.blog.com/2011/02/22/decorator-5/
不,它不正确。您可以向任何您喜欢的类添加接口,如下所示:
type
IAdapter = interface
procedure DoSomething;
end;
TAdapter = class(TBaseClass, IInterface, IAdapter)
private
FRefCount: Integer;
procedure DoSomething;
protected
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
end;
function TAdapter.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TAdapter._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end;
function TAdapter._Release: Integer;
begin
Result := InterlockedDecrement(FRefCount);
if Result = 0 then
Destroy;
end;
procedure TAdapter.DoSomething;
begin
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)