Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Interfaces and Collection Classes in Delphi


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;

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.