I am working on a very basic driving simulation. I am trying to decide the relationship between the following objects: Freeway, Vehicle, Driver, ProximitySensors.
My real-world analysis suggests the following relationships:
A. Freeway has a vehicle: because a freeway can have multiple cars and a car can only have one freeway
B. Vehicle has a driver: because a vehicle can (usually) only have one driver and a driver can (usually) only have on vehicle
C. Vehicle has proximity sensors: only a vehicle can have apparatus for detecting nearby vehicles
However, when beginning to code this up, I've noticed a few oddities I want to straighten out. Here are the constructors I have come up with:
public Freeway(Vehicle car)
public Vehicle(Freeway freeway, Driver driver)
public ProximitySensors(Car car) // In order for it directly access the particular car's position
public Driver()
A lot of these are based on convenience / ease, so I am sure that I'm taking the shorter/incorrect approach. Here are a few questions I have encountered:
First of all, I feel like the Driver should be controlling the vehicle, but as you can see from my other questions, I may be asking the Vehicle to ask the Driver to change lanes instead of the other way around.
Often, I want the proximity sensors to access the freeway based on the vehicle's position (to detect other nearby vehicles), however with this structure, a Freeway has a vehicle and so I'm not sure how the proximity sensors (through the vehicle) will access the freeway unless I pass it to the vehicle as well.
Does the car request permission from the Freeway to change positions? I wanted the car to be independent of the freeway and for it to have an accident if not programmed/performed correctly.
What function should the Driver play exactly? They have a name, age, etc. but should they be the ones to call the proximity sensors on behalf of the car? Should the car do it directly?
Should the Driver have its own method changeLanes(), which calls changeLanes() from the Vehicle which then calls its own proximity sensor function checkSide() which then operates on the Freeway?
When I started to code this, the relationships became murky without every object having access to just about every other object.