When we model position of things in code, we often put the position within the thing being modeled - car, game character, window, etc. So you can do things like: aCar.position - but I think that might be conceptually wrong and have been thinking that for a long time but haven't had much chance to explore the idea.
But of course, you can have this API and delegate the responsibility of calculating the position:
class Street {
has 'cars';
method add_car(){
$car = Car->new( street => $self );
$self->cars->add($car);
return $car;
}
}
class Car {
has 'street';
method position() {
$self->street->position_of($self)
}
}
Now, given a random Car, you can still ask "$car->position", but the actual calculation is handled by something else.
(The deeper issue here is how consumers of Car are not coupling themselves to a particular position-calculation strategy. Change the storage of the position value from the Street to the Car, and consumers are none the wiser. This is the point of OOP.)
But of course, you can have this API and delegate the responsibility of calculating the position:
Now, given a random Car, you can still ask "$car->position", but the actual calculation is handled by something else.(The deeper issue here is how consumers of Car are not coupling themselves to a particular position-calculation strategy. Change the storage of the position value from the Street to the Car, and consumers are none the wiser. This is the point of OOP.)