Delphi:更好的设计以避免引用循环单位? [英] Delphi: better design to avoid circular unit reference?

查看:81
本文介绍了Delphi:更好的设计以避免引用循环单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi 10中我有一个三角形网格结构。

I have a triangular mesh structure in Delphi 10.

出于性能原因,我将网格顶点,三角形面等数据存储在TList的后代中。

For performance reasons I store the data of the mesh's vertices, triangle faces, etc. in descendants of TList.

我让TList对列表的每个成员进行计算。对于这些计算,我需要访问TMesh结构的某些字段。因此,在创建TMesh以及随后创建列表的过程中,我将父TMesh分配给列表。我使用TMesh的前向声明来这样做。请参见以下代码:

I let the TLists do the calculations for every member of the list. For these calculations I need access to some fields of the TMesh structure. Therefore during the creation of TMesh and subsequently the creation of the lists I assign the parent TMesh to the lists. I use a forward declaration of TMesh to do so. Please see the following code:

type
  {forward declaration}
  TMesh=class;

  TVertex=record
    Point: TPoint3D;
    //other fields
  end;

  TVertices=class(TList<TVertex>)
    Mesh: TMesh;
    procedure DoSomethingWithAllVertices; //uses some fields of TMesh
    constructor Create(const AMesh: TMesh);
    //other methods
  end;

  TTriangleFace=record
    Vertices: Array[0..2] of Integer;
    //other fields
  end;

  TTriangleFaces=class(TList<TTriangleFace>)
    Mesh: TMesh;
    procedure DoSomethingWithAllTriangleFaces; //uses some fields of TMesh
    constructor Create(const AMesh: TMesh);
    //other methods
  end;

  TMesh=class(TComponent)
    Vertices: TVertices;
    TriangleFaces: TTriangleFaces;
    constructor Create(AOwner: TComponent);
    //other fields & methods
  end;

implementation

constructor TMesh.Create(AOwner: TComponent);
begin
  inherited;
  Vertices:=TVertices.Create(Self);
  TriangleFaces:=TTriangleFaces.Create(Self);
end;

constructor TVertices.Create(const AMesh: TMesh);
begin
  Mesh:=AMesh;
end;

这很好。

但是我的项目在增长,我得到越来越多的代码,我想将列表类分配到不同的单元中。这就导致了循环单位引用的问题。

However since my project is growing I am getting more and more code and I want to distribute the list classes in separate units. This results in the problem of circular unit references.

循环单位引用的问题似乎是众所周知的。我检查了可能的解决方案,但找不到适合我的问题的解决方案。有人说,如果遇到循环单位引用,则代码设计不当。

The problem of circular unit references seems quite well known. I checked for possible solutions but I cannot find one which seem to fit my problem. Some say that if you run into circular unit references the code is poorly designed.

如何改善设计并同时保持较高的计算性能?

How can I improve the design and at the same time keep the calculation performance high?

还有什么其他方法可以解决该问题?

What are other possibilities to solve the problem?

非常感谢!

推荐答案

您当前的解决方案已经是解决问题的最佳方法。将这些类型分成单独的单元会造成很大的障碍,并会导致代码笨拙。

Your current solution is already the best way to solve the problem. Splitting these types into separate units creates significant obstacles and will lead to unwieldy code.

虽然我可以理解您将这些类型分开的愿望,但是您必须在这种愿望与最终代码的清晰度之间取得平衡。在这种情况下,分拆的负面后果会影响积极的一面。保持原样的代码。

Whilst I can understand your desire to split these types apart, you have to balance that desire against the clarity of the resulting code. In this case the negative consequences of splitting far out weigh the positives. Leave the code as it is.

这篇关于Delphi:更好的设计以避免引用循环单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆