Interface Implementation By Delegation

last modified: July 13, 2004

InterfaceImplementation of a ClassInterface with an ObjectProperty.

Related with InterfaceAggregation

In the DelphiLanguage this is provided by implements keyword


Explain how this is different from other languages or types of inheritance, or DeleteThisPageSoon.


type

  IXInterface = interface(IUnknown)
  ['{713252E5-4636-11D5-B572-00AA00ACFD08},']
     procedure XStaticMethod;
     procedure XVirtualMethod;
  end;

  IYInterface = interface(IUnknown)
  ['{713252E6-4636-11D5-B572-00AA00ACFD08},']
     procedure YMethod;
  end;

  IZInterface = interface(IUnknown)
  ['{713252E4-4636-11D5-B572-00AA00ACFD08},']
  end;


type
  TInnerObject = class(TAggregatedObject,IXInterface,IYInterface)
  public
     procedure XStaticMethod;
     procedure XVirtualMethod; virtual;
     procedure YMethod;
  end;

  TSpecialObject = class(TInnerObject,IXInterface,IYInterface)
  public
     procedure XStaticMethod;
     procedure XVirtualMethod; override;
     procedure YMethod;
  end;

  TFoo = class(TObject,IXInterface,IYInterface,IZInterface)
  private
     FInnerX: TInnerObject;
  protected
   function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
   function _AddRef: Integer; stdcall;
   function _Release: Integer; stdcall;
   function GetX: TInnerObject; virtual;
   function GetY: IYInterface;
  public
     constructor Create;
     destructor  Destroy; override;
     property  InnerX: TInnerObject read GetX implements IXInterface;
     property  InnerY: IYInterface  read GetY implements IYInterface;
  end;

  TBar = class(TFoo,IXInterface,IYInterface,IUnknown)
  private
   FX: TSpecialObject;
   FY: IYInterface;
  protected
     function GetX: TInnerObject; override;
  public
     constructor Create;
     destructor  Destroy; override;
     property Y: IYInterface  read FY implements IYInterface;
     property  X: TSpecialObject read FX implements IXInterface;
  end;


Loading...