Topic: good news from delphi : delegation support


Author: "=?iso-8859-1?B?TG/vYyBUculnYW4=?=" <loic.tregan@hol.fr>
Date: 1998/08/01
Raw View
here is a copy of the chapter concerning a build-in feature of
the new release of Delphi 4.

Hope it may give some ideas ...

Question : may I call this dynamic multi-inheritance (altough new interfaces
are not added, the implementations are changed ) ?

Thanks for your attention.


------------------------------------
Delegating to an interface-type property

If the delegate property is of an interface type, that interface, or an
interface from which it derives, must occur in the ancestor list of the
class where the property is declared. The delegate property must return an
object whose class completely implements the interface specified by the
implements directive, and which does so without method resolution clauses.
For example,

type

  IMyInterface = interface
    procedure P1;
    procedure P2;
  end;

  TMyClass = class(TObject, IMyInterface)

    FMyInterface: IMyInterface;
    property MyInterface: IMyInterface read FMyInterface implements
IMyInterface;
  end;

var

  MyClass: TMyClass;
  MyInterface: IMyInterface;
begin
  MyClass := TMyClass.Create;
  MyClass.FMyInterface := ...  // some object whose class implements
IMyInterface
  MyInterface := MyClass;
  MyInterface.P1;
end;

------------------------------------
Delegating to a class-type property

If the delegate property is of a class type, that class and its ancestors
are searched for methods implementing the specified interface before the
enclosing class and its ancestors are searched. Thus it is possible to
implement some methods in the class specified by the property, and others in
the class where the property is declared. Method resolution clauses can be
used in the usual way to resolve ambiguities or specify a particular method.
An interface cannot be implemented by more than one class-type property. For
example,

type

  IMyInterface = interface
    procedure P1;
    procedure P2;
  end;
  TMyImplClass = class
    procedure P1;
    procedure P2;
  end;
  TMyClass = class(TInterfacedObject, IMyInterface)
    FMyImplClass: TMyImplClass;
    property MyImplClass: TMyImplClass read FMyImplClass implements
IMyInterface;
    procedure IMyInterface.P1 = MyP1;
    procedure MyP1;

  end;
procedure TMyImplClass.P1;
  ...
procedure TMyImplClass.P2;
  ...
procedure TMyClass.MyP1;
  ...
var
  MyClass: TMyClass;
  MyInterface: IMyInterface;
begin
  MyClass := TMyClass.Create;
  MyClass.FMyImplClass := TMyImplClass.Create;
  MyInterface := MyClass;
  MyInterface.P1;        // calls TMyClass.MyP1;
  MyInterface.P2;        // calls TImplClass.P2;
end;
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]