Delphi - 从类和接口继承(适配器模式)?

发布于 2024-10-24 01:49:40 字数 361 浏览 3 评论 0原文

我正在尝试执行 GoF 适配器模式,在 C# 示例中,我遵循的 Adapter 类继承了原始类和一个适配接口。 据我所知,在Delphi(2007)中,这是不可能的,或者是吗?因为如果一个类继承一个接口,它需要从 TInterfacedObject 继承,并且由于 Delphi 不允许多个类继承,这就是故事的结尾。我无法同时继承自定义类和接口。

我说得对吗?

谢谢。

我已经在 http://delphipatterns.blog.com/2011/ 上实现了此模式02/22/装饰器-5/

I am trying to do the GoF adapter pattern and in the C# example that I am following the Adapter class is inheriting the original class and an adapting interface.
In Delphi (2007), as far as I know, this is not possible, or is it? Cause if a class is inheriting an interface, it needs to inherit from TInterfacedObject and since Delphi doesn't allow multiple class inheritance, that is the end of story. I cannot inherit from a custom class and an interface at the same time.

Am I correct?

Thank you.

I have implemented this pattern on http://delphipatterns.blog.com/2011/02/22/decorator-5/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2024-10-31 01:49:40

不,它不正确。您可以向任何您喜欢的类添加接口,如下所示:

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;

No that it not correct. You can add an interface to any class you like as follows:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文