//在平时的应用中,经常会碰到一些,对象集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)];