I am creating entities for a simulation using aggregation and composition.
In the following C++ example:
class CCar
{
CCar( CDriver* pDriver )
{ m_pDriver = pDriver; }
CDriver* m_pDriver;
CEngine m_Engine;
CDriverControls m_Controls;
};
in the above example, a car consists of an engine and a set of driving controls (by composition). A car must also have a driver (by aggregation).
But this only explains the hierarchial relationships - a driver belongs to a car, and an engine and controls also belong to the car. But these members all relate to each other also - a driver must perform an action on the controls, the controls must perform actions on the engine. These relationships also work in multiple directions - the engine can stall and cause the controls to seize up, or the controls could spin wildly and hurt the driver? And what if the driver doesnt like the sound of the engine and leaves the car? How do these relationships work?
I am compositing many different entities from many different objects which often interact with other objects, and am interested in how to manage these relationships in a designed way.
thankyou!
edit:
as responses suggest, one way to manage this is through pointing the car to the driver, and giving the driver a pointer to the car, etc. This makes sense and solves this specific example. However, in a design sense, this increases responsibility of the driver, where this object is tasked with keeping track of which car it belongs to, but surely this is the duty of the container to keep track of which objects belong together? Likewise, tasking CCar with managing these relationships will turn CCar into a blob. Is there a designed solution to dealing with these kinds of relationships?