综合

img qiubolecn

双对象列表,双字符串列表

发表于2004/12/30 8:17:00  802人阅读

//在平时的应用中,经常会碰到一些,对象集TAs中每个A对象都对应每个B对象,对象TBs中每个对象B都对应一个A对象.  如果在A和B中各放一个Tobject属性来进行互相引用,也是非常不错的,但那样依赖关系太强了.
//同样,我们经常会碰到一些这样的情况,有一串的编号和一串的名称进行对应,我们经常要从编号找名称,或从名称找编号.
//所以,此处提取出来了两个对象TDoubleObjList和TDoubleString,虽然可以从它们上面抽象出一个基类,但由于在delphi中string的内存管理机制有点别扭,不属于强类型, 所以还是分开两个对象了.
完整代码如下:
unit DoubleObject;

interface
uses
  Contnrs, Classes;

type
  TDoubleObjList = class
  private
    ObjectList1: TObjectList;
    ObjectList2: TObjectList;
    FOwnsObjects: Boolean;
    procedure SetOwnsObjects(const Value: Boolean);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    property OwnsObjects: Boolean read FOwnsObjects write SetOwnsObjects;
    procedure AddData(AObject1, AObject2: TObject);

    function IndexOfData1(AIndex: Integer): TObject; overload;
    function IndexOfData1(AObject: TObject): Integer; overload;

    function IndexOfData2(AIndex: Integer): TObject; overload;
    function IndexOfData2(AObject: TObject): Integer; overload;

    function Data2GetData1(AObject: TObject): TObject;
    function Data1GetData2(AObject: TObject): TObject;

    function Count: Integer;
  end;

  TDoubleString = class
  private
    Strings1: TStringList;
    Strings2: TStringList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    procedure AddString(AString1, AString2: string);

    function IndexOfData1(AIndex: Integer): string; overload;
    function IndexOfData1(AString: string): Integer; overload;

    function IndexOfData2(AIndex: Integer): string; overload;
    function IndexOfData2(AString: string): Integer; overload;

    function Data2GetData1(AString: string): string;
    function Data1GetData2(AString: string): string;

    function Count: Integer;
  end;
implementation

{ TDoubleObjList }

procedure TDoubleObjList.AddData(AObject1, AObject2: TObject);
begin
  ObjectList1.Add(AObject1);
  ObjectList2.Add(AObject2);
end;

procedure TDoubleObjList.Clear;
begin
  ObjectList1.Clear;
  ObjectList2.Clear;
end;

function TDoubleObjList.Count: Integer;
begin
  Result := ObjectList1.Count;
end;

constructor TDoubleObjList.Create;
begin
  FOwnsObjects := False;
  ObjectList1 := TObjectList.Create;
  ObjectList2 := TObjectList.Create;
end;

function TDoubleObjList.Data1GetData2(AObject: TObject): TObject;
begin
  Result := ObjectList2.Items[IndexOfData1(AObject)];
end;

function TDoubleObjList.Data2GetData1(AObject: TObject): TObject;
begin
  Result := ObjectList1.Items[IndexOfData2(AObject)];
end;

destructor TDoubleObjList.Destroy;
begin
  ObjectList1.Free;
  ObjectList2.Free;
  inherited;
end;

function TDoubleObjList.IndexOfData1(AObject: TObject): Integer;
begin
  Result := ObjectList1.IndexOf(AObject);
end;

function TDoubleObjList.IndexOfData1(AIndex: Integer): TObject;
begin
  Result := ObjectList1.Items[AIndex];
end;

function TDoubleObjList.IndexOfData2(AObject: TObject): Integer;
begin
  Result := ObjectList2.IndexOf(AObject);
end;

function TDoubleObjList.IndexOfData2(AIndex: Integer): TObject;
begin
  Result := ObjectList2.Items[AIndex];
end;

procedure TDoubleObjList.SetOwnsObjects(const Value: Boolean);
begin
  FOwnsObjects := Value;
  ObjectList1.OwnsObjects := Value;
  ObjectList2.OwnsObjects := Value;
end;

{ TDoubleString }

procedure TDoubleString.AddString(AString1, AString2: string);
begin
  Strings1.Add(AString1);
  Strings2.Add(AString2);
end;

procedure TDoubleString.Clear;
begin
  Strings1.Clear;
  Strings2.Clear;
end;

function TDoubleString.Count: Integer;
begin
  Result := Strings1.Count;
end;

constructor TDoubleString.Create;
begin
  Strings1 := TStringList.Create;
  Strings2 := TStringList.Create;
end;

function TDoubleString.Data1GetData2(AString: string): string;
begin
  Result := Strings2.Strings[IndexOfData1(AString)];
end;

function TDoubleString.Data2GetData1(AString: string): string;
begin
  Result := Strings1.Strings[IndexOfData2(AString)];
end;

destructor TDoubleString.Destroy;
begin
  Strings1.Free;
  Strings2.Free;
  inherited;
end;

function TDoubleString.IndexOfData1(AIndex: Integer): string;
begin
  Result := Strings1.Strings[AIndex];
end;

function TDoubleString.IndexOfData1(AString: string): Integer;
begin
  Result := Strings1.IndexOf(AString);
end;

function TDoubleString.IndexOfData2(AString: string): Integer;
begin
  Result := Strings2.IndexOf(AString);
end;

function TDoubleString.IndexOfData2(AIndex: Integer): string;
begin
  Result := Strings2.Strings[AIndex];
end;

end.

阅读全文
0 0

相关文章推荐

img
取 消
img