How would one go about designing classes for a system in which two components depend on each other?
For a more concrete example, consider this scenario, you're designing a piece of software to manage students, teachers and classes in an educational facility. For convenience purposes, you'd like your Class
object to be able to query for the Student
s that are enrolled within its instance. At the same time, you'd like to know what Class
es a Student
is enrolled in.
A simple design may have include a container that holds instances of type Class
in the Student
object, and a container that holds instances of Student
in the Class
object. It does the job but it introduces a cyclic dependency between both classes. Usually, this is looked down upon [citation needed], but it sounds like a valid design where this could be beneficial.
Is there a reason why it would be a bad idea to do so? And how could this situation be rectified without introducing significant performance overhead?