views:

159

answers:

1

I'm trying to create some classes that allow me to retrieve and manipulate a set of backups my application will create and manage.

I've come up with the following code (not tested yet), but I'm not sure if it's the best way of accomplishing this or if there's any easier/better way. I'm using Delphi 2010.

I have a class that holds the backup details (TBackupItem), then I need to have a class that will hold a collection of TBackupItem and finally I'll have a class that manages the reading and writing of backups and also exposes a property that accesses the collection of TBackupItem.

type
  TBackupItem = class
  private
    FBackupProgram: string;
    FBackupProgramVersion: string;
    // There are more variables and properties but for the sake of simplicity I've listed only two
  public
    property BackupProgram: string read FBackupProgram write FBackupProgram;
    property BackupProgramVersion: string read FBackupProgramVersion write FBackupProgramVersion;
  end;

  TBackupsList = class(???)
  private
    // This class will hold a list of TBackupItem. What should I use to accomplish this?
  end;

  TBackupsManager = class(TObject)
  private
    FBackups: TBackupsList;
  public
    property Backups: TBackupsList read FBackups write FBackups;
  end;

Do you guys have any comments and/or examples on the best way of accomplishing this?

Thanks!

+10  A: 

In Delphi 2010 the best approach would be to use Generics.

Use this instead of your custom class TBackupsList:

TBackupsManager = class
private
  FBackups: TObjectList<TBackupItem>;

(You have to include Generics.Collections).

If your collection needs further methods, you can keep TBackupsList and inherit it from TObjectList<TBackupItem>:

TBackupsList = class(TObjectList<TBackupItem>)      
public 
  function MyAdditionalMethod: Integer;
end;

EDIT: If you want to use a custom TBackupsList, but don't want to have all the public methods TObjectList has, you cannot use inheritance. Instead you have to use composition:

TBackupsList = class
private
  FItems: TObjectList<TBackupItem>
public 
  //Duplicates of the original TObjectList public methods accessing FItems.
end;
DR
Thanks, that worked out exactly how I needed. A quick side question (related to this one), any idea if it's possible to not publish all the TObjectList properties to the class I'm deriving it from?
smartins
Updated my answer.
DR