Помогите с оптимизацие классов, убрать дублирующийся код: есть два класса, по сути списки определенных record'ов. Вот код:
| Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Contnrs;
type TRecA = record IdA: Integer; end; PRecA = ^TRecA;
TListA = class private FList: array of TRecA; FCapacity: Integer; FCount: Integer; function GetItems(Index: Integer): TRecA; public constructor Create(ACapacity: Integer = 100); destructor Destroy; override; function Add(AIdA: Integer): Integer; function Count: Integer; property Items[Index: Integer]: TRecA read GetItems; default; end;
TRecB = record ParentA: Integer; Name: string[255]; end; PRecB = ^TRecB;
TListB = class private FList: array of TRecB; FCapacity: Integer; FCount: Integer; function GetItems(Index: Integer): TRecB; public constructor Create(ACapacity: Integer = 100); destructor Destroy; override; function Add(AParentA: Integer; AName: string): Integer; function Count: Integer; property Items[Index: Integer]: TRecB read GetItems; default; end;
TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } FlistA: TlistA; FlistB: TlistB; public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
{ TlistA }
function TListA.Add(AIdA: Integer): Integer; begin Inc(FCount); if FCount > FCapacity then begin Inc(FCapacity, FCapacity); SetLength(FList, FCapacity); end; Result := FCount - 1; FList[Result].IdA := AIdA; end;
function TListA.Count: Integer; begin Result := FCount; end;
constructor TListA.Create(ACapacity: Integer); begin Assert(ACapacity > 0); FCount := 0; FCapacity := ACapacity; SetLength(FList, FCapacity); end;
destructor TListA.Destroy; begin Finalize(FList); inherited Destroy; end;
function TListA.GetItems(Index: Integer): TRecA; begin Assert(Index < FCount); Result := FList[Index]; end;
{ TListB }
function TListB.Add(AParentA: Integer; AName: string): Integer; begin Inc(FCount); if FCount > FCapacity then begin Inc(FCapacity, FCapacity); SetLength(FList, FCapacity); end; Result := FCount - 1; FList[Result].ParentA := AParentA; Flist[Result].Name := AName; end;
function TListB.Count: Integer; begin Result := FCount; end;
constructor TListB.Create(ACapacity: Integer); begin Assert(ACapacity > 0); FCount := 0; FCapacity := ACapacity; SetLength(FList, FCapacity); end;
destructor TListB.Destroy; begin Finalize(FList); inherited Destroy; end;
function TListB.GetItems(Index: Integer): TRecB; begin Assert(Index < FCount); Result := FList[Index]; end;
procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin FlistA := TListA.Create; FlistA.Add(1); FlistA.Add(2);
FlistB := TlistB.Create; FlistB.Add(1, 'I'); FlistB.Add(1, 'P'); FlistB.Add(2, 'S');
for i := 0 to FlistA.FCount - 1 do ShowMessageFmt('%d', [FlistA.Items[i].IdA]); for i := 0 to FlistB.FCount - 1 do ShowMessageFmt('%d; %s', [FlistB.Items[i].ParentA, FlistB.Items[i].Name]); end;
procedure TForm1.FormDestroy(Sender: TObject); begin FlistA.Free; FlistB.Free; end;
end.
|
|