I have been thinking about it for a while and searching online and I don't get to find a clear answer to the questions I am about to write down.
First things first: what am I trying to do?
I am trying to model a machine (a physical asset of a factory) using object oriented programming. To put it simple, let's say that the machine only have an id and a related zone. In this model, I would have two classes (i ommit all the irrelevant properties for this example):
- Machine. [Properties: id, zone]
- Zone. [Properties: id, name]
In this situation, I have to decide what to do about the "zone" property of the Machine Class. As far as I am concerned, the options I have in relation to this property are:
- The property "zone" must contain only the id of the zone in which the machine is located.
- The property "zone" must contain a reference to a full "Zone" object.
In the first case, I have no doubt about what to do inside client code (Instanciating a Zone object and pass the machine "zone"(id) property to the Zone object so it can fetch the zone information inside that object).
But in case I choose the second option, I have some doubts regarding the usage of the Machine class in relation to the Zone class
- Should I instantiate an empty Zone object and pass it to the Machine object? If so, should I fecth the zone information (using the machine id) before passing it to the Machine object? Or, should I call the Zone Class fetching methods inside the Machine Class constructor?
- Or maybe, I should only call those Zone fetching methods in case they are absolutely needed (ie, when I use the getZone method of the Machine class) ? In this case, should I use a static method to return a Zone object inside the "zone" property directly?
Conclussions: If I have to instantiate only one class and zone, I find the first option (zone only containing the id) a better one. But, when I have to deal with more than one instance of Zone and Machine, I guess it is a more clear option to put a full Zone object inside the Machine class.
Am I wrong?
Thank you very much.