8

几个月前我正在编写一堆代码,现在我正在向其中添加内容。我意识到我写了一堆函数,这些函数来自一个类,其中大约 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 年后我从来没有维护过任何东西,是否值得花时间以这种方式修剪你的代码(即删除刚刚继承的函数)并将您的基类函数从抽象更改为具体)

4

3 回答 3

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;

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

第二个选项是:

 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;

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

当然,这都是边缘情况,它们之间还有空间。您可以混合使用这些概念。

只记得:

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

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

于 2010-09-27T17:03:11.760 回答
1

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

于 2010-09-27T16:13:33.707 回答
1

是的,修剪你的代码。

它使您的其他代码更易于阅读(正如您已经提到的,更容易看到实际覆盖了哪些方法)。作为一个额外的好处,在父类中更改方法的签名会更容易:假设您决定将一个参数传递给虚拟方法;您对父类进行更改,然后您需要对从给定父类继承的每个子类重复相同的更改。在这一点上,您不希望只是调用“继承”的虚假覆盖方法!

于 2010-09-28T10:21:24.737 回答