lundi 4 mars 2019

Good object oriented practice to use an object to build itself? [on hold]

Do you think its a good OO practice to use parts of an object to build itself? Here is an example.

var car = new Car 
    {
        Make  = BuildMake(),
        Model = BuildModel(),
        Vin   = BuildVin(),
    };


car.Wheel = BuildWheel(Car.Vin);

In the case above, I had to move the wheel out of the initialize because I need the data from Vin to get it. Is there anything bad with this approach? or would a better approach be

var car = new Car 
    {
        Make  = BuildMake(),
        Model = BuildModel(),
        Vin   = BuildVin(),
        Wheel = BuildWheel(BuildVin())
    };

But in the case above, we will have to call BuildVin() twice which means we are duplicating effort. From a good object oriented design perspective, what approach would one recommend?

Aucun commentaire:

Enregistrer un commentaire