SE450: Some advice: Where to put the code [15/16] |
The real world is full of redundancy and circular references. "Mutual experience" and "common knowledge" are standard parts of human experience, but are completely foreign to OO programming. In OO programming, you are trying to eliminate redundancy and circularity as much as possible, since these are well-known sources of bugs and maintainability issues. So computer models don't do things the way people do. Compare with our discussion of the "Observer" pattern. It is often better to let the one with the knowledge make the choice. When the required knowledge for a decision is spread over two (or more) objects, the art is to decide how these objects should interact. In the case of cars and roads/lights: the car knows its brake distance and max velocity, the road/light knows whether the brake distance matters and what the "next" road/light is. Since the road's behavior depends on its actual class, it make sense to put the code that resolves the choice in the road, rather than the car.