2010-09-27 23 views
8

几个月前我正在写一堆代码,现在我正在向它添加东西。我意识到我写了一大堆函数,这些函数从一个具有大约2/3的函数抽象类的剩余1/3虚拟类中下来。Delphi:可维护性虚拟与虚拟摘要

我非常生病视宁度:

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(即删除刚刚继承的函数在他们和改变你的基类功能从抽象到具体)

回答

1

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

+0

我只是想知道,如果我的耐火材料实际上是耐火材料,或只是干预。大多数情况下,将代码保持原样并让Delphi自动完成我的函数并继承,让Delphi在未实现抽象函数时提醒我会更简单。我改变它的唯一原因是因为当我添加新的子类时,我正在复制并粘贴一个巨大的界面部分(这只是越来越大, – 2010-09-27 16:24:03

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; 

这里有一个基类中的所有类。在那个类中,你可以有共同的属性或事件其他类等等。但是所有的过程都被标记为抽象的,因为它们不执行任何常见的任务。摘要确保它们将在派生类中实现,但不需要在每个类中实现“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. 摘要是一个很好的工具,因此您不必为空的存根使用没有真正需要的过程。另一方面,他们强迫你在派生类中实现它们。
  3. 当你有必须由每个类实现的通用代码并确保OP和DRY干净原则的时候,虚拟就派上用场了。当你有不是每个派生类都有或需要的程序时,他们也是受欢迎的。

对不起,很长的回答,但我不能在这里给一个简单的解释,因为没有。这一切都取决于手头的问题。它是派生类共有多少和它们的实现有多不同之间的一种平衡。

1

是的,修剪你的代码。

它使你的其他代码更容易阅读(如你已经提到的,它会更容易看到什么方法实际上被覆盖)。作为一个附加好处,它将更容易地改变父类中方法的签名:想象一下,您决定将一个参数传递给一个虚拟方法;您对父类进行更改,然后您需要为从给定父类继承的每个子类重复相同的更改。那时你不希望被称为“继承”的假覆盖方法!