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!
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With