Lots of Delphi programmers made, or are making the move, of classic Delphi based containers like TObjectList
into generic containers like TList<T>
.
A while ago, I got into a project that needed to extend lifetime of some objects. Virtually all of them were interface based, and most of the code was from the non-Unicode era, and most of the developers there had a strong background in that era, so they started fiddling with TList
, found it hard, then thought “maybe TList<IInterface>
” where will help.
The problem however, is that Delphi has no IList<T>
. For that, you have to go to the Spring4D library.
Then I sat down with them, and proposed to use an instance good old TInterfacedList
of which the context was maintained in an IInterfacedList
field.
Back in the days where Delphi did not support non-generic types, TInterfacedList
was the only built-in way to store interface references, and the Collection Classes framework by Ray Lischner were the only ways to do that in a more structured way (as they were based on interfaces, an idiom that Embarcadero should have used for their generic collections as well; Spring4D did, so use those collection classes and interfaces whenever possible as they are way more versatile than the Delphi built-in ones)
- Classic
IInterfacedList
example:
- Collection Classes articles:
Back to using TInferfacedList
, as it can still be useful today in:
unit InterfacesHolderUnit;
interface
uses
DebuggableInterfacedObjectUnit, System.Classes;
type
IInterfacesHolder = interface
procedure Add(const aReference: IInterface);
end;
TInterfacesHolder = class(TInterfacedObject, IInterfacesHolder)
strict private
FInterfaces: IInterfaceList;
public
constructor Create();
procedure Add(const aReference: IInterface);
end;
implementation
{ TInterfacesHolder }
procedure TInterfacesHolder.Add(const aReference: IInterface);
begin
FInterfaces.Add(aReference);
end;
constructor TInterfacesHolder.Create();
begin
inherited Create();
FInterfaces := TInterfaceList.Create();
end;
end.
and some tests:
Read the rest of this entry »