I am new convert to Spring4D. I have been experimenting with it by converting some exiting code and I came across this:
{Using System.Generics.Collections}
TDenonInputList = class(TObjectList<TDenonInput>)
private
function GetInputByName(Name: string): TDenonInput;
function GetInputByRename(Name: string): TDenonInput;
public
constructor Create;
destructor Destroy; override;
procedure LockedAdd(ADenonInput: TDenonInput);
procedure LockedClear;
property Names[Name: string]: TDenonInput read GetInputByName;
property Renames[Name: string]: TDenonInput read GetInputByRename;
end;
So, I converted it by doing this:
{Using Spring4D.Collections.Lists}
IDenonInputList = interface(IList<TDenonInput>)
['{2201FB6B-1B9C-4FEE-9614-EFF2A7D35957}']
function GetInputByName(Name: string): TDenonInput;
function GetInputByRename(Name: string): TDenonInput;
procedure LockedAdd(ADenonInput: TDenonInput);
procedure LockedClear;
property Names[Name: string]: TDenonInput read GetInputByName;
property Renames[Name: string]: TDenonInput read GetInputByRename;
end;
TDenonInputList = class(TList<TDenonInput>, IDenonInputList)
private
function GetInputByName(Name: string): TDenonInput;
function GetInputByRename(Name: string): TDenonInput;
procedure LockedAdd(ADenonInput: TDenonInput);
procedure LockedClear;
public
constructor Create;
destructor Destroy; override;
end;
And then I create the new list type by doing this:
var
InputList: IDenonInputList;
begin
InputList := TDenonInputList.Create;
{...}
end;
However, I read that one should use the factory functions in TCollections to create Spring4D lists, such as TCollections.CreateList<T>
. Although what I have done seems to work fine, my question is: Is there a more correct way to create an instance of IDenonInputList, or is there a more proper way to create IList descendents and add additional functions/properties to an IList<T>
?
The factory functions from TCollections
are for creating the built-in collections as they perform code folding in various ways to avoid code bloat in the binary. If you inherit from those classes you are free to create them however you like. But keep in mind that if you make some excessive use of them your binary might grow more in size than if you were using the built-in factory functions. If you want to apply the same code folding techniques as in Spring you need to write similar factory functions as in TCollections
.
Also, keep in mind that I consider the classes in the Spring.Collections.* units as an implementation detail and might change them whenever needed without taking special care that any code outside of Spring that inherits from them still works.