微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Delphi:可维护性虚拟与虚拟摘要

几个月前我写了一堆代码,现在我正在添加一些代码.我意识到我写了一堆函数,这些函数来自一个类,它有大约2/3的函数抽象和剩下的1/3虚函数.

我非常厌倦看到:

function descendent.doSomething() : TList;
begin
   inherited;
end;

当我为基类得到这个:

function descendent.doSomething() : TList;
begin
   result := nil;
end;

并且不愿意结束:

function descendent.doSomething() : TList;
begin

end;

然后想知道为什么有些东西不起作用.

我喜欢使用抽象函数,因为编译器会告诉您是否因为没有实现某些函数而导致抽象错误.

我的问题是,因为我仍然是一个相对较新的Delphi程序员而且我从未需要维护任何8年,所以值得以这种方式花费时间到prune your code(即删除刚刚在其中继承的函数并更改你的基类函数从抽象到具体)

解决方法

这一直取决于问题.我使用接口来定义类集的用户界面.至少当我知道我将有多个底层实际类的实现时.例如,你可以有这样的东西:
IAllInterfaced = interface(IInterface)
    procedure ImplementMeEverywhere_1(const Params: TParams);
    procedure ImplementMeEverywhere_2(const Params: TParams);
    procedure ImplementMeEverywhere_3(const Params: TParams);
  end;

  TAllInterfaced_ClassA = class(TInterfacedobject,IAllInterfaced)
  public
    procedure ImplementMeEverywhere_1(const Params: TParams);
    procedure ImplementMeEverywhere_2(const Params: TParams);
    procedure ImplementMeEverywhere_3(const Params: TParams);
  end;

  TAllInterfaced_ClassB = class(TInterfacedobject,IAllInterfaced)
  public
    procedure ImplementMeEverywhere_1(const Params: TParams);
    procedure ImplementMeEverywhere_2(const Params: TParams);
    procedure ImplementMeEverywhere_3(const Params: TParams);
  end;

在这里,你没有共同的祖先.每个类只实现接口,并且没有公共基类形式的公共底层结构.如果实现是如此不同以至于它们不共享任何东西,那么这是可能的,但是他自己接口.您仍然需要使用相同的接口,以便与派生类的用户保持一致.

第二种选择是:

IAllAbstract = interface(IInterface)
    procedure ImplementMeEverywhere_1(const Params: TParams);
    procedure ImplementMeEverywhere_2(const Params: TParams);
    procedure ImplementMeEverywhere_3(const Params: TParams);
  end;

  TAllAbstract_Custom = (TInterfacedobject,IAllAbstract)
  private
    ...
  public
    procedure ImplementMeEverywhere_1(const Params: TParams); virtual; abstract;
    procedure ImplementMeEverywhere_2(const Params: TParams); virtual; abstract;
    procedure ImplementMeEverywhere_3(const Params: TParams); virtual; abstract;
  end;

  TAllAbstract_ClassA = class(TAllAbstract_Custom)
  public
    procedure ImplementMeEverywhere_1(const Params: TParams); override;
    procedure ImplementMeEverywhere_2(const Params: TParams); override;
    procedure ImplementMeEverywhere_3(const Params: TParams); override;
  end;

  TAllAbstract_ClassB = class(TAllAbstract_Custom)
  public
    procedure ImplementMeEverywhere_1(const Params: TParams); override;
    procedure ImplementMeEverywhere_2(const Params: TParams); override;
    procedure ImplementMeEverywhere_3(const Params: TParams); override;
  end;

这里有一个所有类的基类.在该类中,您可以拥有公共属性或事件其他类等…但是所有过程都标记为抽象,因为它们不执行任何常见任务. Abstract确保它们将在派生类中实现,但是您不需要在每个类中实现“FieldA”,只能在“TAllAbstract_Custom”中实现它.这确保了使用DRY原理.

最后一个选项是:

IAllVirtual = interface(IInterface)
    procedure ImplementMeEverywhere_1(const Params: TParams);
    procedure ImplementMeEverywhere_2(const Params: TParams);
    procedure ImplementMeEverywhere_3(const Params: TParams);
  end;

  TAllVirtual_Custom = (TInterfacedobject,IAllVirtual)
  private
    ...
  public
    procedure ImplementMeEverywhere_1(const Params: TParams); virtual;
    procedure ImplementMeEverywhere_2(const Params: TParams); virtual;
    procedure ImplementMeEverywhere_3(const Params: TParams); virtual;
  end;

  TAllVirtual_ClassA = class(TAllVirtual_Custom)
  public
    procedure ImplementMeEverywhere_1(const Params: TParams); override;
    procedure ImplementMeEverywhere_2(const Params: TParams); override;
    procedure ImplementMeEverywhere_3(const Params: TParams); override;
  end;

  TAllVirtual_ClassB = class(TAllVirtual_Custom)
  public
    procedure ImplementMeEverywhere_1(const Params: TParams); override;
    procedure ImplementMeEverywhere_2(const Params: TParams); override;
    procedure ImplementMeEverywhere_3(const Params: TParams); override;
  end;

这里所有派生类都有一个共同的基本虚拟过程.这可确保您不必在派生类的级别上实现每个过程.您只能覆盖代码的某些部分或根本不覆盖代码的某些部分.

当然这都是边缘情况,beetwen有空间.您可以混合使用这些概念.

只记得:

>接口是强大的工具,可确保您隐藏用户的实现,并且您有一个共同的使用点(接口).它们还强制使用一些规范,因为接口需要全部实现.
> Abstract是一个很好的工具,因此您不必在没有实际需要的过程中使用空存根.另一方面,它们强制您在派生类中实现它们.
>当您拥有必须由每个类实现的公共代码并确保干净的OP和DRY原则时,Virtual会派上用场.当您拥有并非每个派生类都有或需要的程序时,也欢迎它们.

很抱歉很长的答案,但我不能在这里给出一个简单的解释,因为没有.这一切都取决于手头的问题.它是派生类共有多少以及它们的实现有多么不同之间的平衡.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都是大家转来转去,原文出处我已经找不到了。这篇文章写的很不错,但最后部分“PermuteFunction 的终极版本”描述的不太清楚,完全按照该文章代码执行,是不行的。可能是作者故意这样做的?本文最后提供修正后的下载地址。原文如下:一、关于API Hook1.什么是API Hook不知道大家是否还记得,在DO
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都是大家转来转去,原文出处我已经找不到了。 这篇文章写的很不错,但最后部分“PermuteFunction 的终极版本”描述的不太清楚,完全按照该文章代码执行,是不行的。需要修改mess.pas中代码才行。其实文中提到的一个结构,代码中并没有使用typePIMAGE_IMPORT_DESCRIPTOR = ^IMA
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c++ 调用, 抽空研究了一下该库的最新版 ,把部分api 翻译成了dephi版的 记录一下 地址 ffmpegvcl.zip
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和EDX)2个变址和指针寄存器(ESI和EDI) 2个指针寄存器(ESP和EBP)6个段寄存器(ES、CS、SS、DS、FS和GS)
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的功能是:将源操作数送到目的操作数中,即:(dst) <--(src) 1.dst和src类型必须匹配,即必须同为字节
有三个API函数可以运行可执行文件WinExec、ShellExecute和CreateProcess。 1.CreateProcess因为使用复杂,比较少用。 2.WinExec主要运行EXE文件。如:WinExec('Notepad.exe Readme.txt', SW_SHOW); 3.ShellExecute不仅可以运行EXE文件,也可以运行已经关联的文件。 首先必须引用shellapi
API原型: Declare Function MoveFileEx& Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) 参数 类型及说明 lpExistingFileName String,欲移
附带通用控件安装方法: ---------- 基本安装 1、对于单个控件,Componet-->install component..-->PAS或DCU文件-->install; 2、对于带*.dpk文件的控件包,File-->Open(下拉列表框中选*.dpk)-->install即可; 3、对于带*.bpl文件的控件包,Install Packages-->Add-->bpl文件名即可; 4
type   TRec=Record     msg:string;     pic:TMemoryStream; end; procedure TForm2.BitBtn1Click(Sender: TObject); var   ms:TMemoryStream;   Rec1,Rec2:TRec;   cc:tmemorystream;   jpg:TJPEGImage; begin   R