0

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.

  • Please see https://stackoverflow.com/questions/130794/what-is-dependency-injection – Ro Achterberg Jul 22 '19 at 16:40
  • @RoAchterberg Thank you. That partially answers some of the secondary doubts. But it doesnt answer wether, in this case, is it better to pass the id or the full Zone object. – Demolition Architect Jul 22 '19 at 16:56
  • I think you're asking: should I pass an id (primitive integer) or pass an object?. Go with an object. If you don't have an instance of `Zone` at the time when you construct your `Machine`, you could pass it in later via a setter. – waterloomatt Jul 22 '19 at 17:46
  • Id, if zone is no concern of machine and machine is already big of it's own. Whoever needs zone of machine then fetch it themselfs. Object, if zone is very very often needed by whoever uses machine. Proxy that can fetch zone on its own if you have supporting library that can handle proxy creation by itself (e.g. some ORM). Would need more details then provided to make a better judgement. – przemo_li Jul 22 '19 at 18:02
  • Well, Machine is actually quite big on its own, but contain a lot of methods that make use of zone information... I guess I'll go with the object. – Demolition Architect Jul 22 '19 at 18:09

0 Answers0