[fpc-pascal] Question about interfaces and patch
memsom
memsom at interalpha.co.uk
Thu Mar 24 15:37:08 CET 2005
Sorry to be late in the conversation...
>> Here is a simple example how it should be used
>>
>> type
>> IReportable = interface
>> function Report: string;
>> // let's say that this should write report and
>> // return status description as string
>> end;
>>
>> ILogged = interface
>> procedure Log(aStr: string);
>> end;
Surely, using Delphi's way of things:
type TMyReport = class(TInterfacedObject, IReportable, ILogged)
... //an implementation of each interface
end;
>> procedure MyXYZPart.DoReport(aRep: IReportable);
>> begin
>> if (aRep <> nil) then begin
>> if (aRep is ILogged) then
>> (aRep as ILogged).Log(aRep.Report)
>> else
>> aRep.Report
>> end;
Again :
procedure MyXYZPart.DoReport(aRep: IReportable);
var
tmp: ILogged;
begin
if (aRep <> nil) then begin
if (aRep.QueryInterface(ILogged, tmp) = S_OK) then
tmp.Log(aRep.Report) //or you could just do the as cast..
else
aRep.Report
end;
> I see what you want, but if you would do
>
> procedure MyXYZPart.DoReport(aRep: TInterfacedObject);
>
> begin
> if (aRep <> nil) and arep is IReportable then begin
> if (aRep is ILogged) then
> (aRep as ILogged).Log(aRep.Report)
> else
> aRep.Report
> end;
Converting interfaces to instances to object instances is a bad idea... it
screws with reference counting, for a start...> This still does not
explain why you would need to do
> AClassInstance := AnInterface;
I think the above is a really bad idea. Especially when the implementing
class is private... e.g.
interface
type
IPublic = interface
procedure whatever;
end;
TFactory = class
function newPublic: IPublic;
end;
implementation
type
TPrivate = class(TInterfacedObject, IPublic)
...
end;
function TFactory.newPublic: IPublic;
begin
Result := TPrivate.Create; // or implement some kind of class
// pooling etc ..
end;
How would you be able to get at the class? Only as TObject or
TInterfacedObject. Bad idea.
Matt
More information about the fpc-pascal
mailing list