Question

Among the terms:

Motorized Vehicle, Car, Motor, Truck

I was thinking the following:

a Car is a Motorized Vehicle, a Car has a Motor, a Truck has a Motor, a Truck is a Motorized Vehicle

Q1: Am I correct in the above relations?

Q2: We now want to relate the word fan... how would that look?

my Attempt: a fan has a motor, a Motorized Vehicle has a motor, a Car is a Motorized Vehicle, a Car has a Motor, a Truck has a Motor, a Truck is a Motorized Vehicle

I need some clarification on this please....

Était-ce utile?

La solution

This can be a good solution to the above mentioned. As a motor can be replaced in a car with a new one so you can keep aggregation in place of composition . Kindly refer the below image solution

Autres conseils

I think that there is no a 100% accurate response for your question without the context.

For example, to separate truck and car in our context, we should be definatelly able to tell, that car and truck behaves differently.

If both car and truck are just simple containers for (for example) "name" and "mass", or owner etc. with exactly the same behaviour in application, then we propably think in terms of specific objects, not classes, which can drive us to implement Vehicle (or just motorized vehicle) class with two instances

Vehicle car = new Vehicle("Car",1200,someMotor);
Vehicle truck = new Vehicle("Truck",3400,someMotor);

That is simple thing with just "terms", but there is also a relationship issue with context. By relations I mean connections. It's almost sure that Motorized vehicle will have a Motor (but remebmer that also can have more that one), but it is a quite much chance, that "motor has a vehicle" (as the owner, espacially in context of database entities and especialy if our context is more concentrated aroud motors than vehicles - for example when we are building system for motor dealer).

There is another tiny thing that can be not so clear for everybody, it's not only "car has a motor" and "truck has a motor" but more as "motorized vehicle has a motor", so car has a motor not just because "car has a motor", but "because it is motorized vehicle!".

Also, going into details (as question is oop tagged), in object oriented terms we should think about class vs interface and we can end up in conclusion, that MotorizedVehicle is more the interface (from which we can get a motor) than class - if our enviroment (like most) don't allow multiple inheritance. This will prevent us from problems like "Obama's car should be both MotorizedVehicle and ArmoredVehicle but I have motorized/armored in separate class hieries".

TL;DR all depends on context, prefer composition and interfaces instead of complicated class hierarchy

[Car, Truck] "is a" [Motorized Vehicle] "has a" [Motor, Fan]

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top