4

我想知道如何按照以下要求扩展具有附加功能的类层次结构:1)我无法触及原始层次结构 2)我需要将新功能开发到不同的单元中

uClasses.pas单元中的以下类层次结构为例:

TBaseClass = class
  ID : Integer;
  Name : String;
end;

TDerivedClass = class(TBaseClass)
  Age : Integer
  Address : String
end;

我想将其他功能附加到类中,例如将自身保存为文本(这只是一个示例)。所以我想象了以下单元uClasses_Text.pas

uses uClasses;

Itextable = interface
  function SaveToText: String;
end;

TBaseClass_Text = class(TBaseClass, Itextable)
  function SaveToText: String;
end;

TDerivedClass_Text = class(TDerivedClass, ITextable)
  function SaveToText: String;
end;

function TBaseClass_Text.SaveToText: String;
begin
  result := Self.ID + ' ' + Self.Name;
end;

function TDerivedClass_Text.SaveToText: String;
begin
  // SaveToText on derived class must call SaveToText from the "BaseClass" and then append its additional fields  
  result := ???? // Call to TBaseClass_Text.SaveToText. Or better, ITextable(Self.ParentClass).SaveToText;
  result := result + Self.Age + ' ' + Self.Address;
end;

如何从 TDerivedClass_Text.SaveToText 中引用 SaveToText 的“基本”实现?也许以某种方式处理界面?

或者,对于这种情况是否存在更好、更清洁的方法?

谢谢,

4

3 回答 3

4

正如大卫所指出的,您不能引用基类中不存在的方法。

使用类助手,可以以另一种方式解决您的问题。第一类助手TBaseClassHelper添加了一个SaveToText函数,第二类助手也是如此TDerivedClassHelper。查看第二个SaveToText功能的实现。它调用inherited SaveToText.

更新 2

OP 需要针对不同SaveTo实现的单独单元。在 David 和 Arioch 的评论的帮助下,类助手可以从其他类助手继承。这是一个完整的例子:

unit uClasses;

type    

  TBaseClass = class
    ID: Integer;
    Name: String;
  end;

  TDerivedClass = class(TBaseClass)
    Age: Integer;
    Address: String;
  end;

unit uClasses_Text;

uses uClasses,uClasses_SaveToText,uClasses_SaveToIni,uClasses_SaveToDB;

type    
  ITextable = interface
    function SaveToText: string;
    function SaveToIni: string;
    function SaveToDB: string;
  end;

  // Adding reference counting through an interface, since multiple inheritance
  // is not possible (TInterfacedObject and TBaseClass) 
  TBaseClass_Text = class(TBaseClass, IInterface, ITextable)
  strict private
    FRefCount: Integer;
  protected
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  end;

  TDerivedClass_Text = class(TDerivedClass, IInterface, ITextable)
  strict private
    FRefCount: Integer;
  protected
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  end;    

implementation

uses Windows;

function TBaseClass_Text.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TBaseClass_Text._AddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TBaseClass_Text._Release: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
  if Result = 0 then
    Destroy;
end;    

function TDerivedClass_Text.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TDerivedClass_Text._AddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;    

function TDerivedClass_Text._Release: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
  if Result = 0 then
    Destroy;
end;

unit uClasses_SaveToText;

interface

uses uClasses;

type    
  TBaseClassHelper = class helper for TBaseClass
    function SaveToText: string;
  end;

  TDerivedClassHelper = class helper for TDerivedClass
    function SaveToText: string;
  end;

implementation

function TBaseClassHelper.SaveToText: string;
begin
  Result := 'BaseClass Text info';
end;

function TDerivedClassHelper.SaveToText: string;
begin
  Result := inherited SaveToText;
  Result := Result + ' DerivedClass Text info';
end;

unit uClasses_SaveToIni;

interface

Uses uClasses,uClasses_SaveToText;

type    
  TBaseClassHelperIni = class helper(TBaseClassHelper) for TBaseClass
    function SaveToIni: string;
  end;

  TDerivedClassHelperIni = class helper(TDerivedClassHelper) for TDerivedClass
    function SaveToIni: string;
  end;

implementation

function TBaseClassHelperIni.SaveToIni: string;
begin
  Result := 'BaseClass Ini info';
end;

function TDerivedClassHelperIni.SaveToIni: string;
begin
  Result := inherited SaveToIni;
  Result := Result + ' DerivedClass Ini info';
end;

unit uClasses_SaveToDB;

interface

Uses uClasses,uClasses_SaveToText,uClasses_SaveToIni;

Type    
  TBaseClassHelperDB = class helper(TBaseClassHelperIni) for TBaseClass
    function SaveToDB: string;
  end;

  TDerivedClassHelperDB = class helper(TDerivedClassHelperIni) for TDerivedClass
    function SaveToDB: string;
  end;

implementation

function TBaseClassHelperDB.SaveToDB: string;
begin
  Result := 'BaseClass DB info';
end;

function TDerivedClassHelperDB.SaveToDB: string;
begin
  Result := inherited SaveToDB;
  Result := Result + 'DerivedClass DB info';
end;

program TestClasses;

uses
  uClasses in 'uClasses.pas',
  uClasses_Text in 'uClasses_Text.pas',
  uClasses_SaveToText in 'uClasses_SaveToText.pas',
  uClasses_SaveToIni in 'uClasses_SaveToIni.pas',
  uClasses_SaveToDB in 'uClasses_SaveToDB.pas';
var
  Textable: ITextable;
begin
  Textable := TDerivedClass_Text.Create;
  WriteLn(Textable.SaveToText);
  WriteLn(Textable.SaveToIni);
  WriteLn(Textable.SaveToDB);
  ReadLn;
end.

更新 1

阅读您关于需要实施的几个方面的评论,SaveToText我提出了一个简单的搭载解决方案:

type
  ITextable = interface
    function SaveToText: String;
  end;
  TMyTextGenerator = class(TInterfacedObject,ITextable)
  private
    Fbc : TBaseClass;
  public
    constructor Create( bc : TBaseClass);
    function SaveToText: String;
  end;

{ TMyTextGenerator }

constructor TMyTextGenerator.Create(bc: TBaseClass);
begin
  Inherited Create;
  Fbc := bc;
end;

function TMyTextGenerator.SaveToText: String;
begin
  Result := IntToStr(Fbc.ID) + ' ' + Fbc.Name;
  if Fbc is TDerivedClass then
  begin
    Result := Result + ' ' + IntToStr(TDerivedClass(Fbc).Age) + ' ' +
      TDerivedClass(Fbc).Address;
  end;
end;

在不同的单元中使用相同的模式实现 TSaveToIni、TSaveToDB 等。

于 2012-08-09T12:56:58.003 回答
1

根据......(不记得那首歌),诚实被高估了。我认为我们中的许多人都高估了继承,并且在解决继承问题而不是组合或委托方面往往太快了。

我真的质疑希望能够将 SaveToFile 方法添加到您希望能够持久保存到文件的每个类中的愿望。

在我看来,类应该不知道不是它们存在的原因的责任。坚持是一种责任,印刷是另一种责任。一个打印类应该负责打印。当然,您不希望打印类成为 if 语句的黄蜂网,以处理您想要打印的每个可感知的类。因此,您定义了一个 Printer 基类,并使用 PeoplePrinter、LocationPrinter 和 WhatPrinter 后代扩展它。每个都可以处理整个类层次结构。

如果您现在正在考虑装饰器模式,那很好,很好。

这个想法是您不现有层次结构创建后代,而是为特定职责创建类和可能的类层次结构。当您想保存现有类的实例时SomeClass.SaveToText,您将实例化 aTSaver并将其传递给要保存的类的实例,而不是调用 。

一个非常幼稚的实现可能如下所示。

type
  TSaver = class(TObject)
    procedure SaveToText; virtual; abstract;
  end;

  TBaseHierarchySaver = class(TSaver)
  private
    FBase: TBaseClass;
  public
    constructor Create(aBase: TBaseClass);
    procedure SaveToText; override;

    class procedure Save(aBase: TBaseClass);
  end;

constructor TBaseHierarchySaver.Create(aBase: TBaseClass);
begin
  FBase := aBase;
end;

class procedure TBaseHierarchySaver.Save(aBase: TBaseClass);
var
  Me: TSaver;
begin
  Me := TBaseHierarchySaver.Create(aBase);
  Me.SaveToText;
end;

procedure TBaseHierarchySaver.SaveToText;
var
  Str: TStrings;
begin
  Str := TStringList.Create;
  try
    Str.Add(Format('%s (%d)', [FBase.Name, FBase.ID]));
    if FBase.InheritsFrom(TDerivedClass) then
    begin
      Str.Add(Format('%d', [TDerivedClass(FBase).Age]));
      Str.Add(Format('%s', [TDerivedClass(FBase).Address]));
    end;
  finally
    Str.SaveToFile('SomeFileName');
    Str.Free;
  end;
end;

我不太喜欢这个。它很脆。我们可以做得更好。

有很多方法可以使上述代码更加灵活和/或提供多态执行。例如,TSaver 可以有一个与 TBaseClass 类相关的匿名方法字典。TSaver.SaveToText 然后可以获得一个 TBaseClass 参数并被实现为执行传递给它的实例的类的每个匿名方法,如果它继承自与该匿名方法相关的类。

type
  TBaseClassClass = class of TBaseClass;
  TAddInfoProc = reference to procedure(aBase: TBaseClass; aStr: TStrings);

  TSaver = class(TObject)
  class var
    FAddInfoClasses: TDictionary<TBaseClassClass, TAddInfoProc>;
  public
    class procedure RegisterAddInfoProc(aBase: TBaseClassClass; 
      aAddInfo: TAddInfoProc);

    class procedure SaveToText(aBase: TBaseClass);
  end;

TSaver.RegisterAddInfoProc(TBaseClass, procedure(aBase: TBaseClass; aStr: TStrings)
  begin
    aStr.Add(Format('%s (%d)', [aBase.Name, aBase.ID]));
  end
);

TSaver.RegisterAddInfoProc(TDerivedClass, procedure(aBase: TBaseClass; aStr: TStrings)
  begin
    aStr.Add(Format('%d', [TDerivedClass(FBase).Age]));
    aStr.Add(Format('%s', [TDerivedClass(FBase).Address]));
  end
);

这将您从继承层次结构中解放出来,但如果您想要多态执行,则可以将其更改为将特定 TBaseClass 后代绑定到“AddInfo”后代的匹配层次结构的字典,其中每个 AddInfo 后代都添加自己的信息:

type
  TAddInfo = class(TObject)
  public
    procedure AddInfo(aBase: TBaseClass; aStr: TStrings); virtual;
  end;

  TDerivedAddInfo = class(TAddInfo)
  public
    procedure AddInfo(aBase: TBaseClass; aStr: TStrings); override;
  end;

procedure TAddInfo.AddInfo(aBase: TBaseClass; aStr: TStrings);
begin
  aStr.Add(Format('%s (%d)', [aBase.Name, aBase.ID]));
end;

procedure TDerivedAddInfo.AddInfo(aBase: TBaseClass; aStr: TStrings);
var
  Derived: TDerivedClass absolute aBase;
begin
  inherited;
  if not aBase.InheritsFrom(TDerivedClass) then Exit;

  aStr.Add(Format('%d', [Derived.Age]));
  aStr.Add(Format('%s', [Derived.Address]));
end;

type
  TBaseClassClass = class of TBaseClass;
  TAddInfoClass = class of TAddInfo;

  TSaver = class(TObject)
  class var
    FAddInfoClasses: TDictionary<TBaseClassClass, TAddInfoClass>;
  public
    class procedure RegisterAddInfoClass(aBase: TBaseClassClass; 
      aAddInfo: TAddInfoClass);

    class procedure SaveToText(aBase: TBaseClass);
  end;

顺便说一句,这看起来很像其他地方提出的类助手方法,但不受任何时候只有一个类助手活动的限制。因此,您可以拥有 TSaver、TPrinter、TMailer 以及您希望能够使用 TBaseClass 做的任何其他事情,但这不是它的主要职责。

哦,顺便说一句,绝对值的上述使用是我能忍受的为数不多的绝对值用例之一。对于硬演员来说,这是一种方便的简写方式,它通过提前退出约束而变得安全,这本身也是我可以忍受的为数不多的提前退出用例之一:-)

于 2012-08-09T21:03:25.293 回答
1

由于 Delphi 不支持类的多重继承,您被推向这样的解决方案:

function BaseClassSaveToText(obj: TBaseClass): string;
begin
  Result := IntToStr(obj.ID) + ' ' + obj.Name;
end;

function TBaseClass_Text.SaveToText: String;
begin
  Result := BaseClassSaveToText(Self);
end;

function DerivedClassSaveToText(obj: TDerivedClass): string;
begin
  Result := BaseClassSaveToText(obj) + IntToStr(obj.Age) + ' ' + obj.Address;
end;

function TDerivedClass_Text.SaveToText: String;
begin
  Result := DerivedClassSaveToText(Self);
end;

DerivedClassSaveToText你想使用inherited关键字,但你不能,因为这两个类不共享必要的共同祖先。

更新: @LU RD 展示了如何使用类助手来完成这一切。就我个人而言,我对班级助手有点过敏。当然,您不希望使用助手可能还有其他原因。例如,如果您使用的是旧版 Delphi,则它们不存在。

于 2012-08-09T12:57:20.970 回答