处理全局共享数据/对象的方法是什么?

huangapple go评论62阅读模式
英文:

What is THE way to handle globaly shared Data/objects

问题

在阅读了成千上万关于全局变量有多糟糕的帖子后,我回到了起点。

这里有一个人为的示例,我希望能够解释我的情况和问题:

  • TCity:拥有一个Building列表
    • TBuilding:是city的一部分 | 拥有确切的2个garages
      • TGarage:是Building的一部分 | 拥有Cars列表
        • TCar:是Garage的一部分 | 拥有Cars的链表
          • TCar1TCar2TCar3:是TCar的后代

我有一个TBuilding的实例,需要被每个其他类访问。

TBuilding类需要被汽车访问。

我应该为TCityTBuilding实例创建一个全局变量,还是还有其他什么我可以做的?

我尝试合并TCityTBuildingTGarage类,但每次尝试时都让我头痛。

在这里有一些经验丰富的人能够指导我走向正确的方向吗?

我正准备创建一个容器类,以在各个单位之间共享所需的实例。但在阅读了很多帖子后,我不确定这是否是处理这个问题的正确方式。

resourcestring怎么样呢?我应该创建一个包含在类之间共享的resourcestring的容器。例如,如果我有大约20个类都需要同一个字符串,这个字符串需要被翻译,那我会将其制作成resourcestring。我应该在哪里收集这样的数据?

英文:

After reading thousands of posts about how bad Global variables are, I got back to where I started from.

Here is a contrived example which I hope can explain my situation and my question:

  • TCity: has a list of Buildings
    • TBuilding: Is part of a city | has exact 2 garages
      • TGarage: Is part of a Building | has a List of Cars
        • TCar: Is part of a Garage | Has a Linked List of Cars
          • TCar1, TCar2, TCar3: Is Descendant of TCar

i have an instance of TBuilding which needs to be accessed by every single other Class.

The TBuilding Class needs to be accessed by the Cars.

Should I make a global variable for the TCity and TBuilding instance, or is there anything else I can do ?

I tried to merge the TCity, TBuilding and TGarage classes, but that gives me headaches everytime I try.

Could some experienced people here lead me in the right direction?

I was about to create a Container Class, to share the needed instances between units. But after reading lots of posts, I wasn't sure that this was the correct way of dealing with this problem.

What about resourcestrings. Should I create a Container with reourcestrings which are shared amongst Classes. For Example if I have about 20 classes where I need the same string every time. The string needs to be translated, so I make a resourcestring out of it. Where do I collect such data?

答案1

得分: 3

你不需要任何全局变量来实现你似乎想要实现的目标。

你只需要为每个对象存储对父类实例的引用。

例如,你可以为你的 TBuilding 类添加另一个名为 City 的字段,在创建时将引用设置为该建筑所属的 TCity 实例。

TBuilding = class(TObject)
private
  FCity: TCity;
protected
  ...
public
  constructor Create(ACity: TCity);
  property City: TCity read FCity write FCity;
end;

...

constructor TBuilding.Create(ACity: TCity);
begin
  inherited;
  FCity := ACity;
end;

然后对 TGarageTCar 也做类似的操作。

通过这样做,你可以通过类似 Car.Garage.Building 的方式从汽车对象中访问建筑对象。

英文:

You don't need any global variable for what you seem to be trying to achieve.

All you need to do is store reference to parent class instance for each object.

For instance you add another field called City to your TBuilding class and upon its creation you set reference to the TCity instance to which this building belongs.

TBuilding = class(TObject)
private
  FCity: TCity;
protected
  ...
public
  constructor Create(ACity: TCity);
  property City: TCity read FCity write FCity;
end;

...

constructor TBuilding.Create(ACity: TCity);
begin
  inherited;
  FCity := ACity;
end;

Then do similar for TGarage, and TCar.

By doing so you get ability to access Building object from a car object with something like this Car.Garage.Building.

huangapple
  • 本文由 发表于 2023年7月6日 20:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定