'Instance variable dependent on another instance variable
Although it's techincally possible I've seen heavy emphasis on not using any method or other logic inside objects constructor. So my question is: is the case the same for making an instance variable being dependent on another instance variable?
A hypothetical example:
public class ArrowTarget {
...
}
public class Arrow {
//this variable is necessary as in order to use Arrow I must provide a target. I could
//alternatively pass ArrowTarget object to each Arrow's method but it seems more chaotic
// as there is only one ArrowTarget object and I can just pass it to constructor
private ArrowTarget arrowTarget;
public Arrow(ArrowTarget arrowTarget) {
this.arrowTarget = arrowTarget;
}
...
}
public class ArrowGym {
private Athlete athlete = new Athlete();
private ArrowTarget arrowTarget = new ArrowTarget();
private Arrow arrow = new Arrow(this.arrowTarget);
}
So my question is specifically about Arrow variable initialization in ArrowGym class. Technically it is working but is there anything wrong with such design? Could this go wrong with it in case of more complex code? Would it be better to just make Arrow object independent of ArrowTarget class and just pass ArrowTarget argument to each method? It is a kind of logic to implement at initialization but it's very trivial one so I am not sure what to think about it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
