这是否是暴露我希望在类中逻辑分组的子方法的正确方法?

时间:2014-02-17 20:38:47

标签: delphi

我有一个基础对象TCustomerDocument。 TCustomerDocument有两组地址,家庭和商业,我可以将其逻辑分组为TCustomerDocument.HomeAddr和TCustomerDocument.BusinessAddr。然后我可以编写像MyCustomerDocument.HomeAddr.Line1这样的代码来从文档中读取家庭住址的第一个地址行。

问题是在TCustomerDocument中,这些地址在另一个类(TDocumentHeader)中:

TCustomerDocument
  TDocumentHeader (address fields are in here)

现在我可以写

MyCustomerDocument.DocumentHeader.GetField('HOMEADDR1').AsString;

访问第一个家庭地址行。

由于TDocumentHeader只有两个地址(家庭和商业),我开始创建一个TAddressInfo对象:

TCustomerDocument
  Home: TAddressInfo
  Business: TAddressInfo
  TDocumentHeader (address fields are in here)

这很好用。代码的相关部分如下:

type
TCustomerDocument = class; //fwd declaration

TAddressInfo = class(TObject)
private
    fDocumentHeader: TCustomerDocument;
    destructor Destroy; override;

public
    constructor Create(aCustomerDocument: TCustomerDocument);
    function Addr1: string;
end;

TCustomerDocument= class(TObject)
public
    HomeAddr: TAddressInfo;
    BusinessAddr: TAddressInfo

然后在TCustomerDocument.Create我有:

HomeAddr := TAddressInfo.Create(Self);
BusinessAddr := TAddressInfo.Create(Self);

[为了简洁,我省略了清理/免费代码。]

我不需要(此时)直接访问TAddressInfo对象 - 我只是想用它来逻辑地对字段进行分组以使我的代码更清晰。

问题:这是构造代码以将这些字段组合在一起的正确方法还是更简单的方法(可能在类中使用记录结构)?

1 个答案:

答案 0 :(得分:0)

您要执行的操作的解决方案是使用TCustomerDocument中的属性来公开TCustomerDocument.DocumentHeader中的地址。

这样的事情(从头到尾):

TAddress = Class(TObject)
private
  LineList: TStringlist;
  protected
    function GetLines(Index: Integer): string;
    procedure SetLines(Index: Integer; const Value: string);
  public
    property Lines[Index: Integer]: string read GetLines write SetLiines;

...
End;

  TCustomerDocumentHeader = class(TObject)
  public
    HomeAddress: TAddress;
    BusinessAddress: TAddress;
...
  end;

  TCustomerDocument = Class(TObject)
  private
    fDocumentHeader: TCustomerDocumentHeader;
    function GetBusinessAddress: TAddress;
    procedure SetBusinessAddress(const Value: TAddress);
    function GetHomeAddress: TAddress;
    procedure SetHomeAddress(const Value: TAddress);
...

  public
    Property DocumentHeader: TCustomerDocumentHeader read fDocumentHeader write fDocumentHeader;
    Property BusinessAddress: TAddress read GetBusinessAddress  write SetBusinessAddress;
    Property HomeAddress: TAddress read GetHomeAddress  write SetHomeAddress;
...
  End;
implementation

function TAddress.GetLines(Index: Integer): string;
begin
  Result := LineList[Index];
end;

procedure TAddress.SetLines(Index: Integer; const Value: string);
begin
  LineList[Index] := Value;
end;


function TCustomerDocument.GetBusinessAddress: TAddress;
begin
  Result := DocumentHeader.BusinessAddress;
end;

function TCustomerDocument.GetHomeAddress: TAddress;
begin
  Result := DocumentHeader.HomeAddress;
end;

procedure TCustomerDocument.SetBusinessAddress(const Value: TAddress);
begin
  DocumentHeader.BusinessAddress := Value;
end;

procedure TCustomerDocument.SetHomeAddress(const Value: TAddress);
begin
  DocumentHeader.HomeAddress := Value;
end;

然后你可以做像

这样的事情
MyDocument.BusinessAddress.Lines[n] := '1 test street';