Home > Mobile >  How to inherit a Interface from foreign Interface without reimplementing the Methods
How to inherit a Interface from foreign Interface without reimplementing the Methods

Time:09-23

I want to create an Interfaced object that supports an Interface from somewhere else my own functions. So, how to stack/aggregate/enhance the Interface? I guess its possible, but I cant find a snippet or demo specific for my inheritance experiment.

This solution is not quite what I want:

TImplement = Class(TInterfacedObject, IOne, ITwo)
private
  FOne: IOne;
public
  property One: IOne read FOne implements IOne;
  property Two: ITwo read FTwo implements ITwo;
end;

Current usage:

(MyInterface as IOne).Something;
(MyInterface as ITwo).SomethingElse;

Desired usage:

MyInterface.Something;
MyInterface.SomethingElse;

I tried inheriting the Interface:

ITogether = Interface(IOne)
  procedure SomeThingElse;
end:

TImplement = Class(TInterfacedObject, ITogether)
// or Class(TInterfacedObject, ITogether, IOne) => Both result in missing Implementation message on compile ... 
private
  FOne: IOne;
  function SomeThingElse;
public
  property One: IOne read FOne implements IOne;
end;

This combination says something like:

E2291 Implementation of Method x From Interface IOne missing.

Is it possible to combine the Interface in a way so that the "cast free" calls are possible?

CodePudding user response:

You can implement the IOne methods and forward them to the FOne interface.

type
  IOne = interface
    ['{19F785C0-5D2E-479F-BB2C-88A00BA4C812}']
    procedure Something;
  end;

  ITogether = interface(IOne)
    ['{B8B7F690-DC98-41AB-A6D9-29F70330EDA5}']
    procedure SomethingElse;
  end;

type
  TTogether = class(TInterfacedObject, ITogether)
  private
    FOne: IOne;
  protected
    property One: IOne read FOne;
  public
    constructor Create(AOne: IOne);
    procedure SomethingElse;
    procedure Something;
  end;

constructor TTogether.Create(AOne: IOne);
begin
  inherited Create;
  FOne := AOne;
end;

procedure TTogether.Something;
begin
  One.Something;
end;

procedure TTogether.SomethingElse;
begin
  { Do something else }
end;

AFAIK, there is no language construct like implements that does that for you when the implementor is an interface property.

CodePudding user response:

Your question appears to be about two things. Firstly it's about calling the methods without having to cast.

Just use the object reference and you can do exactly that.

  MyObject:=TImplements.Create;
  MyObject.Something;
  MyObject.SomethingElse;

Secondly it's about implementing an interface without having to re-implement the functions.

Delphi Interfaces, by their definition, cannot include implementations. (The methods have to be abstract, or in C terms they are 'pure virtual').

This means that you cannot do a multiple-inheritance type implementation like you can with C . Any object implementing an interface must implement all of the implementing functions ... or ...

You can delegate an interface to a property as in your example, and if you do that you can still call the methods without casting if you use the object reference.

  • Related