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

Pet*_*ner 8 delphi maintainability refactoring coding-style

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

我非常厌倦看到:

function descendent.doSomething() : TList;
begin
   inherited;
end;
Run Code Online (Sandbox Code Playgroud)

当我为基类得到这个:

function descendent.doSomething() : TList;
begin
   result := nil;
end;
Run Code Online (Sandbox Code Playgroud)

并且不愿意结束:

function descendent.doSomething() : TList;
begin

end;
Run Code Online (Sandbox Code Playgroud)

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

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

我的问题是,因为我仍然是一个相对较新的Delphi程序员而且我从来没有必要维护任何8年的事情,是否值得花时间以这种方式修剪你的代码(即删除其中继承的函数)并将基类功能从抽象更改为具体)

Run*_*ner 7

这一直取决于问题.我使用接口来定义类集的用户界面.至少当我知道我将有多个底层实际类的实现时.例如,您可以这样:

 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;
Run Code Online (Sandbox Code Playgroud)

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

第二种选择是:

 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;
Run Code Online (Sandbox Code Playgroud)

这里有一个所有类的基类.在该类中,您可以拥有公共属性或事件其他类等...但是所有过程都标记为抽象,因为它们不执行任何常见任务.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;
Run Code Online (Sandbox Code Playgroud)

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

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

只记得:

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

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


Mas*_*ler 1

如果代码真的很简单,并且您发现很难阅读并且容易出错,那么它可能就很难阅读并且容易出错。(另一方面,如果代码很复杂,你发现很难阅读,这可能是你缺乏经验。但不是这样的。)你现在可能会很好地重构它,而问题仍然存在新鲜在你的脑海中。