TEnglish = class(TInterfacedObject, ISpeak) public
procedure sayHello; stdcall; end;
//e.g. the dog with procedure TDog.sayHello; begin ShowMessage('dog is barking wauwau'); end;
{
Now we add the instances of the interface in the list, using the defined type
TInterfaceList so we are able to ask with QueryInterface if an object supports
an Interface, in our example if a dog as an object can kiss or just sayhello:
}
procedure TForm1.btnCollectClick(Sender: TObject); var collection: TInterfaceList;
i: Integer;
aObjspeak: ISpeak;
aObjKiss: IKiss; begin collection := TinterfaceList.Create; try
with collection do
begin Add(TEnglish.Create);
Add(TFrench.Create);
Add(TDog.Create); end; for i := 0 to collection.Count - 1 do
begin aObjSpeak := collection[i] as ISpeak; //TFrench, TEnglish, TDog if aObjSpeak nil then aObjSpeak.sayHello;
collection[i].queryInterface(IKiss, aObjKiss); //only TFrench if aObjKiss nil then aObjKiss.kiss; end; finally collection.Free; end; end;