Interfaces and Collection Classes in Delphi
By Ray Lischner, December 01, 2001
Delphi has been traditionally weak when it comes to collections. Using interfaces, it is now possible to write useful, type-safe collection classes in Delphi. Ray examines an interface-based set of Collection classes and provides a red-black tree implementation as an example.
HexWeb HTML
Example 2: Declaring the TCollection class type
// As a convenience for implementing collection classes, use TCollection
// as an abstract base class. It implements several of the methods in a
// generic way.
TCollectionClass = class of TCollection;
TCollection = class(TInterfacedObject, ICollection)
protected
// The following method has an inefficient implementation, so
// derived classes should override it, if possible.
function GetCount: Integer; virtual;
// The default is for collections to accept duplicate members.
// Derived classes should override the following methods if they
// give the programmer control or do something different.
function GetDuplicates: TDuplicates; virtual;
procedure SetDuplicates(Dup: TDuplicates); virtual;
procedure DuplicateError(Item: ICollectible);
procedure ItemNotFound(Item: ICollectible);
procedure ValueNotFound(Item: ICollectible);
procedure IndexOutOfRange(Index: Integer);
procedure NotImplemented(const MethodName: string);
public
constructor Create; virtual;
// Derived classes must override the following four methods:
procedure Add(Obj: ICollectible); overload; virtual; abstract;
procedure Enumerator(out Enum: IEnumerator); overload; virtual; abstract;
function Find(const SearchFor: ICollectible; out Found: ICollectible):
Boolean; virtual; abstract;
procedure Remove(const Obj: ICollectible); virtual; abstract;
// The following methods have inefficient implementations, so derived
// classes should override them, if possible.
procedure Clear; virtual;
procedure RetainAll(const Collection: ICollection); virtual;
// The default implementation of the following methods always returns False.
// Override any or all of them to return True when appropriate.
function IsMap: Boolean; virtual;
function IsSorted: Boolean; virtual;
function IsOrdered: Boolean; virtual;
// The following have convenient implementations. Most derived classes
// will not need to override these methods, but can do so if the derived
// class has a better way of doing the same thing.
procedure AddAll(Collection: ICollection); virtual;
procedure Clone(out Collection: ICollection); virtual;
function Contains(const Obj: ICollectible): Boolean; virtual;
function ContainsAll(const Collection: ICollection): Boolean; virtual;
function CountMatching(const Obj: ICollectible): Integer; virtual;
procedure First(out Item: ICollectible); virtual;
procedure ForEach(Proc: TCollectionProc); overload; virtual;
function IsEmpty: Boolean; virtual;
function IsEqual(const Collection: ICollection): Boolean; virtual;
procedure RemoveAll(const Obj: ICollectible); overload; virtual;
procedure RemoveAll(const Collection: ICollection); overload; virtual;
function ToArray: TCollectibleArray; virtual;
property Count: Integer read GetCount;
property Duplicates: TDuplicates read GetDuplicates write SetDuplicates;
end;
// Add all items from Collection to Self.
procedure TCollection.AddAll(Collection: ICollection);
var
Enum: IEnumerator;
Item: ICollectible;
begin
Collection.Enumerator(Enum);
while Enum.Next(Item) do
Add(Item);
end;
// Delete all the items from the collection. You should usually override this
// method to use a more efficient implementation.
procedure TCollection.Clear;
var
Item: ICollectible;
Enum: IEnumerator;
begin
Enumerator(Enum);
while Enum.Next(Item) do
Enum.Remove;
end;
// Return True if the collection contains Obj.
function TCollection.Contains(const Obj: ICollectible): Boolean;
var
Item: ICollectible;
begin
Result := Find(Obj, Item);
end;
// Return True if the collection contains all of the items in Collection.
function TCollection.ContainsAll(const Collection: ICollection): Boolean;
var
Enum: IEnumerator;
Item: ICollectible;
begin
Result := False;
Collection.Enumerator(Enum);
while Enum.Next(Item) do
if not Contains(Item) then
Exit;
Result := True;
end;