Let suppose we have 2 classes,
Plane, being in an optional one-to-one relationship. So a
Plane might have a pilot when it's flying, but when it's standing in its hangar, it has none. Similar for the
It would be just logical to hold a pointer to the
Plane in the
Pilot and a pointer to the
Pilot in the
Plane, accessible via getters and setters.
Constructing the setters so that a call on any of them establishes the bidirectional relationship is surprisingly hard. I was even more surprised that I couldn't find a suitable solution on the web as this construct seems to be common to me.
Does anyone know of concepts that would encapsulate such a relationship inside a class? Or an idiom to stick with normal getters/setters and still establish the relationship with one setPilot/setPlane call?