gpt4 book ai didi

Delphi:可维护性虚拟与虚拟抽象

转载 作者:行者123 更新时间:2023-12-03 14:47:42 26 4
gpt4 key购买 nike

几个月前我写了一堆代码,现在我正在向其中添加一些东西。我意识到我编写了一堆函数,这些函数源自一个类,该类的函数大约有 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以这种方式(即删除刚刚继承的函数并将基类函数从抽象更改为具体)

最佳答案

一如既往,这取决于问题。我使用接口(interface)来定义一组类的用户界面。至少当我知道我将拥有多个底层实际类的实现时。例如你可以有这样的东西:

 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;

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

第二个选项是:

 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;

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

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

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

请记住:

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

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

关于Delphi:可维护性虚拟与虚拟抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3805516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com