0

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?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Dennis
  • 15
  • 4

1 Answers1

3

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.

SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • Thanks for the explanation. I have a TManager now which holds instances of the Data Class. The Data Class is a Class combined with all of my other Classes except the SubClass like Tcar1, TCar2 etc. I handle the refferencing with constructor injection. now if i add a TConfig instance to my manager which needs to be accessed by all of the other Classes i have i get again a circular dependency or did i do somnething wrong again ? – Dennis Jul 07 '23 at 06:05
  • What do you mean by _circular dependency_? Can you edit your question in order to show roughly how you implemented your object right now? – SilverWarior Jul 07 '23 at 10:06
  • i managed to solve the circular dependency with creating strong and weak dependencies. Howerver i still don't know in which unit i should put resourcestrings that need to be accessed by multiple classes. Should i create a new unit just to hold those resourcestrings ? – Dennis Jul 10 '23 at 05:45
  • If those resource strings are shared between multiple units then creating a new unit for them and have other units reference to it might be a god approach. You can find quite a lot of information about resource string in [Is there some advantage in use resourcestring instead of a const string?](https://stackoverflow.com/q/4292611/3636228) question here on SO. – SilverWarior Jul 10 '23 at 11:41