加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:03:54 所属栏目:大数据 来源:网络整理
导读:几个月前我写了一堆代码,现在我正在添加一些代码.我意识到我写了一堆函数,这些函数来自一个类,它有大约2/3的函数抽象和剩下的1/3虚函数. 我非常厌倦看到: function descendent.doSomething() : TList;begin inherited;end; 当我为基类得到这个: function d
几个月前我写了一堆代码,现在我正在添加一些代码.我意识到我写了一堆函数,这些函数来自一个类,它有大约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会派上用场.当您拥有并非每个派生类都有或需要的程序时,也欢迎它们.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读