Einzelnen Beitrag anzeigen

Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.445 Beiträge
 
Delphi 10.4 Sydney
 
#14

Re: Auf Interfaces zugreifen?

  Alt 23. Feb 2010, 16:50
Falls du das bis jetzt überlesen hast: NIEMALS! Objektvariablen und Interfacevariablen für ein und das selbe Objekt.

Wenn deine Faktory die Objekte erzeugen soll, die IAppInterface unterstützen, dann gehören die Units die diese Objekte deklarieren, nirgends sonst eingebunden. Die konkrete Implementation geht niemanden etwas an, maximal die Faktory kennt diese, um die Objekte zu erzeugen. Die Faktory liefert IAppInterface, also sind alle Variablen außerhalb auch IAppInterface.

Hier mal eine Skizze wie eine Faktory funktionieren könnte:
Delphi-Quellcode:
unit UAppIntfDef;

type
  TCustomAppInterface = class(TInterfacedObject, IAppInterface)
    constructor Create; virtual; // virtualer Constructor für die Faktory
    procedure DoSomething; virtual; abstract;
  end;

  TAppInterfaceClass = class of TCustomAppInterface;

implementation


{********************}

unit UAppIntfFactory;

uses
   UAppIntfDef;

function RegisterAppInterface(AClass: TAppInterfaceClass);
procedure GetAppInterfaces(AItems: TStrings);
function GetAppInterface(AName: string): IAppInterface;

implementation

var
  FClasses: TClassList;

function RegisterAppInterface(AClass: TAppInterfaceClass);
begin
  FClasses.Add(AClass);
end;

procedure GetAppInterfaces(AItems: TStrings);
begin
  AItems.Clear;
  for i := 0 to FClasses.Count - 1 do
  begin
    AItems.Add(FClasses[i].ClassName;
  end;
end;

function GetAppInterface(AName: string): IAppInterface;
begin
  for i := 0 to FClasses.Count - 1 do
  begin
    if FClasses[i].ClassName = AName then
    begin
      Result := TAppInterfaceClass(FClasses[i]).Create;
      Exit;
    end;
  end;
end;

{********************}

unit UAppIntfImpl1;

interface

implementation

uses
  UAppIntfDef, UAppIntfFactory;

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

  procedure TAppIntfImpl.DoSomething;
  begin

  end;

begin
  RegisterAppInterface(TAppIntfImpl);
end.

{********************}

unit UAppIntfImpl2;

interface

implementation

uses
  UAppIntfDef, UAppIntfFactory;

type
  TAppIntfImp2 = class(TInterfacedObject, IAppInterface)
    constructor Create; override;
    procedure DoSomething;
  end;

  constructor TAppIntfImp2.Create;
  begin

  end;

  procedure TAppIntfImp2.DoSomething;
  begin

  end;

begin
  RegisterAppInterface(TAppIntfImp2);
end.

{********************}

unit UAppIntfUser;

type
  TForm1 = class(TForm)

  private
    FInterface: IAppInterface;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FInterface := GetAppInterface('TAppIntfImp1');
end;

procedure TForm1.RadioButtonClick(Sender: TObject);
var
  s: string;
begin
  if Sender = RadioButton1 then s := 'TAppIntfImp1'
  else if Sender = RadioButton2 then s := 'TAppIntfImp2'
  else
    Exit;

  FInterface := GetAppInterface(s);
end;

procedure TForm1.ButtonClick(Sender: TObject);
begin
  FInterface.DoSomething;
end;
Classname ist natürlich keine schöne Unterscheidung der Klassen, aber dafür kann man in TCustomAppInterface ja ein eigenes class Property / class Methode vorsehen.
  Mit Zitat antworten Zitat