Implement a Delphi interface using an 'anonymous' class

Viewed 3103

I have an interface.

type IProgressObserver = interface(IInterface)
    procedure ReportProgress(Progress:Integer);
    procedure ReportError(Message:string);
end;

I have implemented the interface using a named class, as follows:

type TProgressObserver=class(TInterfacedObject, IProgressObserver)    
    procedure ReportProgress(Progress:Integer);
    procedure ReportError(Message:string);
end;

... implementation of methods go here .....

addProgressObserver(TProgressObserver.Create);

Is it possible to create an instance of this interface without declaring a class? Something like this (imaginary) code, that would do the same thing as above:

 addProgressObserver(IProgressObserver.Create()
 begin
   procedure ReportProgress(Progress:Integer)
   begin
     ShowMessage('Progress Observed!');
   end

   procedure ReportError(Message:string)
   begin
     Log(Message);
   end
 end;);

Delphi has anonymous procedures, but does it have anonymous classes??

I found this similar question, but it's in Java.

I am using Delphi 2010

2 Answers
Related