I tried to learn OOP methods and design patterns for a while, and my approach has certainly improved, but I don’t quite click when we talk about objects of interacting classes. I mainly work in PHP, but I would accept a general answer for the sake of learning. And to apologize in advance, if I use the conditions incorrectly, I'm self-taught here. Here is an example:
Suppose I want to simulate people moving in a car. There are 3 classes:
class Car {}
class Driver {}
class Road {}
and I want the Car class to have a function
public function Drive($time) {}
in which driving should update the driver’s location, depending on the speed limit of the road. My question is the best way to structure Drive so that the car interacts with other objects. I see 3 possibilities from the head:
public function Drive($time, $Driver, $Road) {}
, , . , , , , , ..
$Car->setDriver($Driver);
$Car->setRoad($Road);
$Car->Drive($time);
, Drive , , . , , , .
class Car() {
public function Drive($time) {
$Driver = getDriver($this->DriverID);
$Road = getRoad($this->RoadID);
}
}
function getDriver($DriverID) {
static $DriverArray;
return $DriverArray[$DriverID];
}
function getRoad($RoadID) {}
, , DriverID RoadID, db. ( , , .) , getDriver, .
SELECT <columns> FROM Car_tbl INNER JOIN Driver_tbl on DriverID INNER JOIN Road_tbl on RoadID, , , OO, , , .
3, , - . PHP , . .
edit: , .
, , , UseCase , UseCase . , , getRoadSpeedLimit(), getMaxCarSpeed() getMaxDriverSpeed(), . , -, , ..
UMLCat, , ( ), , ?
class RoadTrip_aka_Universe {
// Operates on instances of Car, Driver, Route (which may contain roads)
}
: $Car, $Driver $Route , , ? , , , ?
, , ? , , , .
.