Einzelnen Beitrag anzeigen

viakt133

Registriert seit: 16. Feb 2010
18 Beiträge
 
Lazarus
 
#1

Auf Interfaces zugreifen?

  Alt 18. Feb 2010, 15:33
Hallo,

ich lese grad im Delphi Treff Forum herum und bin dabei auf Interfaces gestoßen. Das hat mich zu folgendem Programmentwurf inspiriert:

Delphi-Quellcode:
unit UAppIntf;

interface

uses
  Classes, Sysutils;

type
  IAppInterface = Interface(IIinterface)
  ['{8FBE82FA-E3BA-4B8D-992D-315965BF5407}']
    procedure DoSomething;
  End;

implementation

end.

//Hier nun die Implementation

unit UAppIntfImpl;

interface

uses
  Classes, Sysutils, Dialogs, UAppIntf;

type
  TAppIntfImpl = class(TInterfacedObject, IAppInterface)
    procedure DoSomething;
  end;

implementation

{ TAppIntfImpl }

procedure TAppIntfImpl.DoSomething;
begin
  ShowMessage('the Interfaced method');
end;

end.

//Und hier nun will ich das Interface verwenden, ohne die Methode DoSomething noch mal
//implementieren zu müssen.

unit UAppIntfUser;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UAppIntf, UAppIntfImpl;

type
  TForm1 = class(TForm)
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

end.
Wie greife ich jetzt auf das Interface zu?

Wäre IAppintfImpl eine Klasse (dann TAppIntfImpl), sähe das ja so aus:

var
Instance: TAppIntfImpl;

begin
Instance := TAppIntfImpl.Create; // evtl. ...create(parameter);
end.

WIe aber mache ich das, wenn ich stattdessen das Interface verwenden will?
  Mit Zitat antworten Zitat