i have ObjectList container and i want to add an internal
iterator (Visitor Pattern) in fact i’m attempting to determine
duplicates in my List..
a sample: http://pastebin.com/pjeWq2uN
this code to provide an insight of what i’m trying to achieve..
TFindDuplicatesMethod = procedure(s1, s2: string) of object;
TPersonList = class(TObjectList)
public
procedure Iterate(pMethode: TFindDuplicatesMethod)
end;
procedure TPersonList.Iterate(pMethode: TFindDuplicatesMethod)
var
i: Integer;
begin
for i := 0 to Count - 1 do
pMethode(TMyClass(Items[i]).S1, {But i don't have the second parameter because
it comes from outside of PersonList Ex: OpenDialog.Files[i]})
end;
function FindDuplicate(S1, S2: string): Boolean;
begin
Result := False;
if S1 = S2 then
Result := True;
end;
begin
Files.Iterate(FindDuplicates(S1, S2));
end;
i’m wondering how OOP solve such problem.
thank’s in advance…
Ok, as we found in comments, we have 2 tasks:
TObjectListalready contains an item (so new item is an duplicate)TImageListto reduce memory usage and store only unique icons.As I mentioned in comments, you should ask about your second question in separate thread, but I suggest you to add new files icons depeding on new file mime-type, instead of binary icon data. Here you have to create file-type dictionary, determine file-type and so on..
What about duplicates in
TObjectList.You probably know, that there is
genericimplemntation ofTObjectList–TObjectsList<T>. As in your example you can defineTPersonListasTObjectList<TPerson>, soitemsproperty always returnsTPersonobjects instance.now, generic task with lists – list sorting. Take a look at
Sort()method ofTObjectList<T>/TList. It has 2 overload methods. One of them is default, and second takes anCompareras parameter. Actually, the first method also uses an comparer – default comparer.So comparer is an implemntation of
IComparer<T>interface wich has the only method –function Compare(l,r : T):integer; Usually you define this sort-comparer at runtime as anonimous method, before calling theSort()method. Using you anonimous method you always know how to compare twoT-typed objects, and then you can determine wich of them is “greater” than other, and should be the first in list.so the same situation you have while searching for duplicates in list. but now you have to determine, are 2 objects equal or not.
Let us suppose you have
personList : TPersonListwich containsTPersoninstances. Each person has, for exmaple, name, surname, date of birth and ID.Of course default comparer knows nothing about how to compare 2 persons. But you can provide new comparer wich knows. For example, let suppose 2 objects are equals, if their IDs are equal;
TPersonconstructor is usual:now we have to write
TPersonListcontructor. As you can see,TObejctListconstructor has few overloads. One of them hasComparerparameter. It savesaComparertoFComparerfield. Now, take a look atContainsmethod. It finds does list already contain object or not. It usesIndexOfmethod. So if returned index = 0 then list contains our object.So now our task is to define new comparer in
TPersonListconstructor. We should define comparsion method, then create comparer object and pass it to List contructor.to test our code, lets add some persons to list.
So, this is the usual way how to determine if
TList(or its descendant) contains object.