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
Antwort Antwort
Seite 2 von 2     12   
mjustin

Registriert seit: 14. Apr 2008
3.006 Beiträge
 
Delphi 2009 Professional
 
#11

Re: Auf Interfaces zugreifen?

  Alt 19. Feb 2010, 12:09
Zitat von sirius:
Bei allen anderen Interfacelösungen muss man wegen der Referenzzählung höllisch aufpassen und sollte es deswegen (meiner Meinung nach) nur sehr selten einsetzen.
Interfaces verwende ich eigentlich 'höllisch gerne'

Prkatisch sind sie zum Beispiel bei der XML API, wenn man nach Belieben eine Library durch eine andere ersetzen kann, da die API einheitlich ist. Wenn ein Anbieter nette Extrafeatures hat, oder die Library besonders schlank ist, muss man nicht erst drei Tage lang refaktorisieren

Und mit Interfaces kann man das gut von der Festlegung auf vorgegebene Klassenhierarchien trennen. Java macht das schon seit Ewigkeiten (JDBC, Mail-API, Servlet-API JMS, ...), und die Hersteller der dazu passenden Implementierungen freuen sich, dass sie eine stabile Vorgabe haben - Design by Contract sozusagen - die den berüchtigten Vendor-Lock-In verhindert. Doch bei Delphi gibt es natürlich auch Teile, die ähnlich arbeiten, dbExpress zum Beispiel, keine Frage.

Wenn Delphi ein Problem mit Interfaces hat, dann eher weil der Anwender sich vertun kann bei der Mischung von Objekt- und Interfacereferenzen, oder wenn TComponent eigene Besitzerverwaltung praktiziert (und man das nicht bedenkt). Fazit: ich liebe Interfaces.

Viele Grüße,
Michael
Michael Justin
  Mit Zitat antworten Zitat
Benutzerbild von sirius
sirius

Registriert seit: 3. Jan 2007
Ort: Dresden
3.443 Beiträge
 
Delphi 7 Enterprise
 
#12

Re: Auf Interfaces zugreifen?

  Alt 19. Feb 2010, 12:15
Zitat von mjustin:
Java macht das schon seit Ewigkeiten
Ja, in Java finde ich Interfaces auch sehr schön. Aber Java hat einen GC, der die Verwendung extrem vereinfacht. Man muss auf gewisse Dinge nicht aufpassen, welche du auch teilweise nennst:
Zitat:
Wenn Delphi ein Problem mit Interfaces hat, dann eher weil der Anwender sich vertun kann bei der Mischung von Objekt- und Interfacereferenzen, oder wenn TComponent eigene Besitzerverwaltung praktiziert (und man das nicht bedenkt). Fazit: ich liebe Interfaces.
Wenn man auf alles Obacht gibt, dann sind Interfaces auch ne tolle Sache. Ober das man in Delphi Obacht geben muss ist minder toll und es kann sehr blöde Fehler geben.
Dieser Beitrag ist für Jugendliche unter 18 Jahren nicht geeignet.
  Mit Zitat antworten Zitat
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
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.439 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
Hawkeye219

Registriert seit: 18. Feb 2006
Ort: Stolberg
2.227 Beiträge
 
Delphi 2010 Professional
 
#15

Re: Auf Interfaces zugreifen?

  Alt 23. Feb 2010, 18:36
Hallo,

die Funktion GetAppInterface sollte auch dann ein definiertes Ergebnis liefern, wenn man ihr einen unbekannten Klassennamen übergibt. Alternativ könnte sie natürlich eine Exception auslösen.

Gruß Hawkeye
  Mit Zitat antworten Zitat
Blup

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

Re: Auf Interfaces zugreifen?

  Alt 24. Feb 2010, 14:48
GetAppInterface gibt hier nil zurück, wenn ein unbekannter Klassenname angegeben wird.
Im Prinzip hast du mit der Exception recht, aber das ist wie gesagt nur eine Skizze.
  Mit Zitat antworten Zitat
Hawkeye219

Registriert seit: 18. Feb 2006
Ort: Stolberg
2.227 Beiträge
 
Delphi 2010 Professional
 
#17

Re: Auf Interfaces zugreifen?

  Alt 24. Feb 2010, 17:16
Hallo Blup,

Zitat von Blup:
GetAppInterface gibt hier nil zurück, wenn ein unbekannter Klassenname angegeben wird.
Bei der Verwendung lokaler Variablen sieht das möglicherweise anders aus:

Delphi-Quellcode:
function F1: IInterface;
begin
  Result := TInterfacedObject.Create;
end;

function F2: IInterface;
begin
end;

procedure P1;
var
  I: IInterface;
begin
  I := F1();
  if Assigned(I) then
    ShowMessage ('nach F1(): I <> nil');

  I := F2();
  if Assigned(I) then
    ShowMessage ('nach F2(): I <> nil');
end;
Die Erklärung dieses Verhaltens liefert Alex Ciobanu in seinem Blog.

Gruß Hawkeye
  Mit Zitat antworten Zitat
Blup

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

Re: Auf Interfaces zugreifen?

  Alt 25. Feb 2010, 08:01
Gut zu wissen, schlecht das der Compiler für diesen Fall keine Warnmeldung gibt.
Delphi-Quellcode:
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;
  Result := nil;
end;
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu
Online

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.223 Beiträge
 
Delphi 12 Athens
 
#19

Re: Auf Interfaces zugreifen?

  Alt 25. Feb 2010, 08:16
@Hawkeye219: Das selbe Verhalten gibt es übrigens auch bei Strings und dynamischen Arrays.

[edit]
ich seh grad, in der verlinkten Seite geht es um die Strings (dachte es geht da um das Interface),
aber ist ja bei allem das Selbe.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


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:13 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz