I have 2 kinds of objects in my system, Car
and Road
.
A Car
can be of various types and have properties like tire, tire size, engine, etc.
A Road
can be of multiple types and have properties like terrain type, slippery, etc.
Some type of cars cannot ride on certain roads, based on the road properties and car limitations/constraints, like a common car cannot drive on beach sand because it would get stuck with smaller tires and weaker engine, but a "monster truck" could, because it has bigger tires and a better engine.
Considering these restrictions, how should the responsibility of checking whether a car can ride on a specific road be determined?
I think that a Car
should throw exceptions if it cannot ride, but I don't think a Car should know in which road is going to ride.
Should the road check the car's properties and tell if it's able to run on the road? I think this options falls to a procedural approach of getters/setters.
How would this scenario "scale" if a had multiple properties on a Car that could affect the capability of riding on a road?
How could I approach this with these interfaces?
export interface Car {
engine: string;
tire: string;
tireSize: string;
ride(): void // ride only if Road is compatible
}
export interface Road {
terrain: string;
isSlippery: boolean;
cars: Car[];
startRace(): void
}