使用’anonymous’类实现Delphi接口

前端之家收集整理的这篇文章主要介绍了使用’anonymous’类实现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

解决方法

你可以匿名,使用匿名方法实现接口.但是你没有得到实际的编译器支持,你必须自己声明所有的匿名方法类型,然后实现实际的“匿名”类.给定您的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中实现匿名方法,它也相当快速有效.

原文链接:https://www.f2er.com/delphi/102519.html

猜你在找的Delphi相关文章