AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Auf Interfaces zugreifen?

Offene Frage von "viakt133"
Ein Thema von viakt133 · begonnen am 18. Feb 2010 · letzter Beitrag vom 25. Feb 2010
 
viakt133

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

Re: Auf Interfaces zugreifen?

  Alt 20. Feb 2010, 15:56
Hallo,

bin wieder bei den Interfaces angekommen. Hab jetzt diesen Ansatz:

Delphi-Quellcode:
unit UAppIntfFactory;

interface

uses
  Classes, Sysutils, Contnrs, UAppIntf;

var
  Interfaces: TInterfaceList; //In DElphi eingebaut
  ClassesIntf: TObjectList; //In DElphi eingebaut

  Interfaced: IAppInterface;

procedure SetClassOf(Instance: TObject);
procedure SetInterfaceOf(Name: String);
function GetInterfaceOf(Name: String): IInterface;
function GetClassOf(Instance: TObject): TObject;

implementation

uses UAppIntfImpl;

procedure SetClassOf(Instance: TObject);
begin
  if Assigned(ClassesIntf) then
  begin
    ClassesIntf.Add(Instance);
  end;
end;

procedure SetInterfaceOf(Name: String);
begin
  if Assigned(Interfaces) then
  begin
    Interfaces.Add(Interfaced);
  end;
end;

function GetClassOf(Instance: TObject): TObject;
var Index: Integer;
begin
  if Assigned(ClassesIntf) then
  begin
    Index := 0;
    while Index < ClassesIntf.Count do
    begin
      {
      if Instance is Classesintf.Items[index] then
      begin
        Result := ClassesIntf.Items[Index];
        Index  := ClassesIntf.Count;
      end;
      }

      Inc(Index);
    end;
  end;
end;

function GetInterfaceOf(Name: String): IInterface;
var Index: Integer;
begin
  if Assigned(Interfaces) then
  begin

    Result := Interfaces.Items[Index];
  end;
end;

initialization
  Interfaces := TInterfaceList.Create;
  ClassesIntf := TObjectList.Create;

finalization
  ClassesIntf.Free;
  Interfaces.Free;

end.

//Hier die Implementation der Klassen:

unit UAppIntfImpl;

interface

uses
  Classes, Sysutils, Dialogs, UAppIntf, UAppIntfFactory;

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

  TAppIntfImpl2 = class(TInterfacedObject, IAppInterface)
    procedure DoSomething;
  end;

var
  InterfacedClass1: TAppIntfImpl;
  InterfacedClass2: TAppIntfImpl2;

implementation

{ TAppIntfImpl }

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

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

initialization
  //InterfacedClass := TAppIntfImpl.Create;
  if Assigned(Interfaces) then
  begin
  Interfaces.Add( InterfacedClass1 );
  Interfaces.Add( InterfacedClass2 );
  end;
  if Assigned(ClassesIntf) then
  begin
    ClassesIntf.Add(InterfacedClass1);
    ClassesIntf.Add(InterfacedClass2);
  end;

  //InterfacedClass.Free;

end.


//Hier kommt die Anwendung:

unit UAppIntfUser;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    cbxIntfChange: TComboBox;
    lbIntfChange: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cbxIntfChangeCloseUp(Sender: TObject);
  private
    { Private-Deklarationen }
    FInterfaceExists: Boolean;
  public
    FInterface: IAppInterface;
    { Public-Deklarationen }
    FInterfaced: IAppInterface;
    FInterface2: TAppIntfImpl;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if FInterfaceExists then
  //FInterfaced.DoSomething;
  FInterface2.DoSomething;
end;

procedure TForm1.cbxIntfChangeCloseUp(Sender: TObject);
var Index: Integer;
begin
  Index := cbxIntfChange.ItemIndex;
  cbxIntfChange.Text := cbxIntfChange.Items.Strings[Index];
  //FInterfaced := Interfaces.Items[Index];
  //------ Hier kommt eine Listen-Index-Exception, die ich mir nicht erklären kann
  FInterface2 := TAppIntfImpl(ClassesIntf.Items[index]);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FInterfaceExists := true;
  //FInterfaced := GetInterfaceOf('Impl2');
end;

end.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:31 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz