I'm new to the concept of object oriented programming (in java) and I often encounter the following design problem:
I often create classes with instance variables which are not known at the moment of initialization. These variables are filled over time. This is not a real problem since these variables are null until they are filled, my question therefore is more about the best practice in such situations.
Let me explain it with an example. I have a class Car
. Every Car
has a color
, vMax
, weight
, horsepower
etc.
When initializing the Car
only it's color
, weight
and horsepower
are known. --> Car(color, weight, horsepower)
The vMax
can now be calculated (let's say: weight/horsepower
). What confuses me is, that the Car
, after initialization is "incomplete", meaning that the vMax
will only be filled over time.
I found this quite ugly, and of course the car-example is simplified. I often have classes with 10+ properties where some are calculated over time, which themselves later in the code are used to calculate even more properties of that object. It then becomes difficult to know which variables are already filled at a certain point and which are not.
I just wondered whether this is "normal" and the way OOP works, or whether such situations should be avoided. If yes, I'd be glad for some design-hints.
Michael
vMax
should be a method rather than a variable... – LatonyavMax
:) – Allyl