Hallo.
Vielen Dank für die Hinweise. Ich habe meine Objektliste bei Add() und Insert() so modifiziert, sodass sie die Objekte dupliziert anstelle nur zu referenzieren. Ich muss mich jetzt beim Umgang mit Add() also nicht mehr explizit um die Duplizierung kümmern. Außerdem kann ich meine Objekte dann sofort freigeben, was bei den Ressourcenschutzblöcken für Übersichtlichkeit sorgt. Ich hoffe, dass mein Code nun professioneller in Sachen
OOP geworden ist.
Delphi-Quellcode:
type
TSubmission = class(TPersistent)
public
Tags: string;
Rating: string;
Score: integer;
PictureNumber: integer;
MD5: string;
procedure Assign(Source: TPersistent); override;
end;
TSubmissionCollection = class(TObjectList)
protected
function getItem(Index: Integer): TSubmission; virtual;
procedure setItem(Index: Integer; Objekt: TSubmission); virtual;
public
function Add(Objekt: TSubmission): Integer; virtual;
function Remove(Objekt: TSubmission): Integer; virtual;
function IndexOf(Objekt: TSubmission): Integer; virtual;
procedure Insert(Index: Integer; Objekt: TSubmission); virtual;
function First: TSubmission; virtual;
function Last: TSubmission; virtual;
property Items[index: Integer]: TSubmission read getItem write setItem; default;
end;
{ TSubmission }
procedure TSubmission.Assign(Source: TPersistent);
begin
if Source is TSubmission then
begin
Tags := TSubmission(Source).Tags;
Rating := TSubmission(Source).Rating;
Score := TSubmission(Source).Score;
PictureNumber := TSubmission(Source).PictureNumber;
MD5 := TSubmission(Source).MD5;
end
else
inherited;
end;
{ TSubmissionCollection }
function TSubmissionCollection.getItem(Index: Integer): TSubmission;
begin
Result := TSubmission(inherited Items[Index]);
end;
procedure TSubmissionCollection.setItem(Index: Integer; Objekt: TSubmission);
begin
inherited Items[Index] := Objekt;
end;
function TSubmissionCollection.Add(Objekt: TSubmission): Integer;
var
duplikat: TSubmission;
begin
duplikat := TSubmission.Create;
duplikat.Assign(Objekt);
Result := inherited Add(duplikat);
end;
function TSubmissionCollection.First: TSubmission;
begin
Result := TSubmission(inherited First());
end;
function TSubmissionCollection.IndexOf(Objekt: TSubmission): Integer;
begin
Result := inherited IndexOf(Objekt);
end;
procedure TSubmissionCollection.Insert(Index: Integer; Objekt: TSubmission);
var
duplikat: TSubmission;
begin
duplikat := TSubmission.Create;
duplikat.Assign(Objekt);
inherited Insert(Index, duplikat);
end;
function TSubmissionCollection.Last: TSubmission;
begin
Result := TSubmission(inherited Last());
end;
function TSubmissionCollection.Remove(Objekt: TSubmission): Integer;
begin
Result := inherited Remove(Objekt);
end;
{ Anwendungsbeispiel }
function Filtere(Input: TSubmissionCollection): TSubmissionCollection;
var
i: integer;
begin
result := TSubmissionCollection.Create;
for i := 0 to Input.Count - 1 do
begin
if odd(i) then
begin
result.Add(Input.Items[i]);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
x, y: TSubmissionCollection;
sub: TSubmission;
i: integer;
begin
x := TSubmissionCollection.Create;
try
for i := 0 to 100 do
begin
sub := TSubmission.Create;
try
sub.MD5 := 'Test'+IntToStr(i);
x.Add(sub);
finally
sub.Free;
end;
end;
y := Filtere(x);
try
finally
y.Free;
end;
finally
x.Free;
end;
end;
Gruß
blackdrake