Delphi - inherit from class and interface (adapter template)?

I am trying to make a GoF adapter template, and with the C # example, which I follow the adapter class, the source class and adaptation interface are inherited. In Delphi (2007), as far as I know, this is not possible, or is it? The reason that a class inherits an interface is that it must inherit from TInterfacedObject, and since Delphi does not allow multiple class inheritance, that is, the end of the story. I cannot inherit from user class and interface at the same time.

Am I right?

Thanks.

I implemented this template at http://delphipatterns.blog.com/2011/02/22/decorator-5/

+3
source share
1 answer

No, that is not right. You can add an interface to any class you like:

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; 
+11
source

All Articles