Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   TList / Interfaces (https://www.delphipraxis.net/212287-tlist-interfaces.html)

ConnorMcLeod 19. Jan 2023 21:54

TList / Interfaces
 
Kommt man da tatsächlich nicht weiter oder seh ich es einfach nicht?
Bitte keine guten Ratschläge zu TList/IList<T>/Spring4D und so weiter weil es sich um eine nicht diskutierbare Vorgabe handelt TList zu verwenden ;-)

Es sei ein Interface
Delphi-Quellcode:
  ISimpleInterface = interface
    function GetID: Integer;
    procedure SetID(AValue: Integer);
    property ID: Integer read GetID write SetID;
  end;
und eine Liste von diesen Dingern:
Delphi-Quellcode:
  TSimpleIntfList = class(TList<ISimpleInterface>)
  public
    function ItemByID(AiID: Integer): ISimpleInterface;
  end;
Eine Erweiterung vom Interface:
Delphi-Quellcode:
  IModule = interface(ISimpleInterface)
    function GetModNo: Integer;
    procedure SetModNo(AValue: Integer);
    property ModNo: Integer read GetModNo write SetModNo;
  end;
Nun hätte ich gerne eine Ableitung von TSimpleIntfList, also
Delphi-Quellcode:
  TModuleList = class(TSimpleInterfaceList<IModule>) // funkt nicht - schon klar
    function ModuleByModNo(AiModNo: Integer): IModule;
  end;
Aber
Delphi-Quellcode:
ItemByID
sollte trotzdem noch funktionieren ...
Geht das?

himitsu 19. Jan 2023 22:17

AW: TList / Interfaces
 
Nja wenn es ginge, dann höchstens so
Delphi-Quellcode:
 TSimpleIntfList<T> = class(TList<T>)
  public
    function ItemByID(AiID: Integer): T;
  end;
oder
Delphi-Quellcode:
 TSimpleIntfList<T> = class(TList<ISimpleInterface>)
  public
    function ItemByID(AiID: Integer): T;
  end;
hier im ItemByID dann ein cast von ISimpleInterface zu T (IModule) , weil die Liste natürlich ISimpleInterface speichert, aber auch den Nachfahren aufnehmen kann.

ConnorMcLeod 19. Jan 2023 23:18

AW: TList / Interfaces
 
Mmh, Zweiteres bringt mich auf eine Idee. Das probier ich gleich nach dem Aufstehen, danke!

ConnorMcLeod 20. Jan 2023 04:23

AW: TList / Interfaces
 
Meine Lösung:
Delphi-Quellcode:
  ISimpleInterface = interface
    function GetID: Integer;
    procedure SetID(AValue: Integer);
    property ID: Integer read GetID write SetID;
  end;
  IModule = interface(ISimpleInterface)
    function GetModNo: Integer;
    procedure SetModNo(AValue: Integer);
    property ModNo: Integer read GetModNo write SetModNo;
  end;
die Liste von diesen Dingern:
Delphi-Quellcode:
  TSimpleIntfList = class(TList<ISimpleInterface>)
  public
    function SimpleItemByID(AiID: Integer): ISimpleInterface;
  end;
eine generische Liste von (beinahe) beliebigem Nachfahrtyp:
Delphi-Quellcode:
  TCustomIntfList<T: ISimpleInterface> = class(TSimpleIntfList)
  strict private
    FrGuid: TGUID;
  public
    constructor Create;
  public
    function TypedItemAtIndex(AiIndex: Integer): T;
  end;
 
uses
    System.SysUtils
  , System.TypInfo
  ;
 
constructor TNntCustomIntfList<T>.Create;
begin
  inherited Create;
  FrGuid := GetTypeData(TypeInfo(T))^.Guid;
end;

function TCustomIntfList<T>.TypedItemAtIndex(AiIndex: Integer): T;
var
  LoItem: ISimpleInterface;
begin
  LoItem := Items[AiIndex];
  Supports(LoItem, FrGuid, Result);
end;
Eine konkrete Nachfahrliste:
Delphi-Quellcode:
  TModuleList = class(TCustomIntfList<IModule>)
    function ModuleByModNo(AiModNo: Integer): IModule;
  end;
 
function ModuleByModNo(AiModNo: Integer): IModule;
var
  LoItem : ISimpleInterface;
  LoModule: IModule;
begin
  for LoItem in Self do begin
    if Supports(LoItem, IModule, LoModule) then begin
      if LoModule.ModNo = AiModNo then begin
        Exit(LoModule);
      end;
    end;
  end;
  Result := nil;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 05: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