Golf course example:
We have the classes Course, Track, Tee, and Hole.
public class Course {
private final List<Track> tracks;
}
public class Track {
private final Course parent;
private final Tee tee;
private final Hole hole;
}
public class Tee {
private final Track parent;
}
public class Hole {
private final Track parent;
}
There is a lot of parenting going on here.
Coursemust consist of 18 tracks, as is normal in golf.Trackmust come with each aTeeandHole, but must also specify a parentCoursewhich it belongs toTeeandHolemust also specify a parent, but an instance ofTrack, to which they each belong to.
But I am running into a problem. When i try to call the constructor of Track...
public Track(Course parent, Tee tee, Hole hole) {//}
... i must pass both an instance of Tee and Hole, but...
public Hole(Track parent) {//}
... the constructors of these demand that I pass an instance of Track. And i'm stuck with a recursive problem.
I could "workaround" this by not making each parent member of these classes final anymore, but that just feels hacky, like bad practice - and there is no reason for them to not be final as they will never change in my use case.
Something else i can think of, is outsourcing "parent-child" relationships like these to a different manager-type class; not too excited about doing something like that either though.
Question
What is the proper way to handle such "parent-child" relationships, with the goal in mind to have them finalized and having simple and quick access to methods like #getParent()?
Thank you