使用'anonymous'类实现Delphi接口

时间:2011-07-22 06:24:17

标签: delphi

我有一个界面。

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

我使用命名类实现了接口,如下所示:

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

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

addProgressObserver(TProgressObserver.Create);

是否可以在不声明类的情况下创建此接口的实例?像这个(虚构的)代码,它会做与上面相同的事情:

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

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

Delphi有匿名程序,但是它有匿名类吗?

我找到了this similar question,但它是用Java编写的。

我正在使用Delphi 2010

2 个答案:

答案 0 :(得分:17)

可以变得非常匿名,使用匿名方法实现接口。但是你没有得到实际的编译器支持,你必须自己声明所有的匿名方法类型,然后实现实际的“匿名”类。给定IProgressObserver接口,实现看起来像这样:

type
  // This is the interface we'll be dealing with.
  IProgressObserver = interface(IInterface)
    procedure ReportProgress(Progress:Integer);
    procedure ReportError(Message:string);
  end;

  // This will help us anonymously create implementations of the IProgressObserver
  // interface.
  TAnonymousObserverImp = class(TInterfacedObject, IProgressObserver)
  type
    // Declare reference types for all the methods the interface needs.
    TReportProgressProc = reference to procedure(Progress:Integer);
    TReportErrorProc = reference to procedure(Message:string);
  strict private
    FReportProgressProc: TReportProgressProc;
    FReportErrorProc: TReportErrorProc;

    // Actual implementation of interface methods.
    procedure ReportProgress(Progress:Integer);
    procedure ReportError(Message:string);
    // private constructor, so we'll forced to use the public "Construct" function
    constructor Create(aReportProgressProc: TReportProgressProc; aReportErrorProc: TReportErrorProc);
  public
    // This takes the required anonymous methods as parameters and constructs an anonymous implementation
    // of the IProgressObserver interface.
    class function Construct(aReportProgressProc: TReportProgressProc; aReportErrorProc: TReportErrorProc): IProgressObserver;
  end;

{ TAnonymousObserverImp }

class function TAnonymousObserverImp.Construct(
  aReportProgressProc: TReportProgressProc;
  aReportErrorProc: TReportErrorProc): IProgressObserver;
begin
  // Call the private constructor
  Result := TAnonymousObserverImp.Create(aReportProgressProc, aReportErrorProc);
end;

constructor TAnonymousObserverImp.Create(
  aReportProgressProc: TReportProgressProc; aReportErrorProc: TReportErrorProc);
begin
  inherited Create;
  // We simply save the references for later use
  FReportProgressProc := aReportProgressProc;
  FReportErrorProc := aReportErrorProc;
end;

procedure TAnonymousObserverImp.ReportError(Message: string);
begin
  // Delegate to anonymous method
  FReportErrorProc(Message);
end;

procedure TAnonymousObserverImp.ReportProgress(Progress: Integer);
begin
  // Delegate to anonymous method
  FReportProgressProc(Progress);
end;

一旦完成所有代码,您就可以编写如下代码:

var i: IProgressObserver;
begin
  i := TAnonymousObserverImp.Construct(
    procedure (Progress:Integer)
    begin
      // Do something with Progress
    end
    ,
    procedure (Message:string)
    begin
      // Do something with Message
    end
  )
end;

对我来说看起来很匿名!鉴于在Delphi中实现匿名方法,它也相当快速有效。

答案 1 :(得分:5)

简短回答我很害怕:抱歉,不,Delphi没有匿名课程。