Thema: Delphi Umgang mit Interfaces

Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#12

AW: Umgang mit Interfaces

  Alt 6. Dez 2013, 09:12
Für deine Zwecke wäre sogar ein generisches TDictionary besser, da du ja irgendwie auf die Objekte per Namen zugreifen willst (siehe TListData.fName).
Mal ein Beispiel, als Keys des Dictonarys (auch Map genannt) kann man vielleicht auch die GUID der Interfaces nehmen:

Delphi-Quellcode:
unit Unit2;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  System.Generics.Collections,
  Vcl.StdCtrls;

type
  IBase = Interface(IUnknown)
    function GetCanSave : Boolean;
  end;

  IValue = Interface(IBase)
    function GetIsNaN : Boolean;
  End;

  IInteger = Interface(IValue)
    function GetValue : Integer;
    procedure SetValue(const AValue : Integer);
  End;

  ICompare = Interface(IBase)
    function Compare : Boolean;
  End;

  ISomeThing = Interface(IBase)
    function DoSomeThing : Integer;
  End;

  TMyBase = Class(TInterfacedObject, IBase)
  public
    function GetCanSave : Boolean;
  End;

  TMyInteger = Class(TMyBase, IInteger)
  public
    function GetValue : Integer;
    procedure SetValue(const AValue : Integer);
    function GetIsNaN : Boolean;
  End;

  TMyCompare = Class(TMyBase, ICompare)
  public
    function Compare : Boolean;
  End;

  TMyDoSomething = Class(TMyCompare, IInteger, ISomeThing)
  public
    function DoSomeThing : Integer;
    function GetValue : Integer;
    procedure SetValue(const AValue : Integer);
    function GetIsNaN : Boolean;
  End;

  TForm2 = class(TForm)
    Button1 : TButton;
    procedure FormCreate(Sender : TObject);
    procedure Button1Click(Sender : TObject);
  private
    { Private declarations }
    fList : TDictionary<string, IBase>;
  public
    { Public declarations }
  end;

var
  Form2 : TForm2;

implementation

{$R *.dfm}

{ TMyCompare }

function TMyCompare.Compare : Boolean;
begin

end;

{ TMyInteger }

function TMyInteger.GetIsNaN : Boolean;
begin
  //
end;

function TMyInteger.GetValue : Integer;
begin
  //
end;

procedure TMyInteger.SetValue(const AValue : Integer);
begin
  //
end;

{ TMyDoSomething }

function TMyDoSomething.DoSomeThing : Integer;
begin
  //
end;

function TMyDoSomething.GetIsNaN : Boolean;
begin
  //
end;

function TMyDoSomething.GetValue : Integer;
begin
  //
end;

procedure TMyDoSomething.SetValue(const AValue : Integer);
begin
  //
end;

{ TMyBase }

function TMyBase.GetCanSave : Boolean;
begin
  //
end;

procedure TForm2.FormCreate(Sender : TObject);
begin
  fList := TDictionary<string, IBase>.Create;

  fList.Add(TMyInteger.ClassName , TMyInteger.Create);
  fList.Add(TMyCompare.ClassName , TMyCompare.Create);
  fList.Add(TMyDoSomething.ClassName , TMyDoSomething.Create);
end;

procedure TForm2.Button1Click(Sender : TObject);
var
  MyObject : IBase;
begin
  if fList.TryGetValue(TMyDoSomething.ClassName, MyObject) then
  begin
    IInteger(MyObject).SetValue(1);
    ISomeThing(MyObject).DoSomeThing;
  end;

  if fList.TryGetValue(TMyInteger.ClassName, MyObject) then
  begin
    IInteger(MyObject).SetValue(2);
  end;

  if fList.TryGetValue(TMyCompare.ClassName, MyObject) then
  begin
    ICompare(MyObject).Compare;
  end;
end;

end.
  Mit Zitat antworten Zitat