Say there are two classes connected to each other through some relationship. For example, a Student maintains a Class list that it accepts, and each Class has a Student list that takes it. Then I’m afraid to let Student directly modify my Class es set, because each modification should be followed by a similar modification to the Class Student s list, and vice versa.
One solution is to have a class whose sole purpose is to keep track of Class - Student relationships, such as Registrar . But then, if any method in Student requires knowledge of its Class list, Student needs to pass Registrar . That seems bad. It seems that Student should not have access to Registrar , where he can also contact other Student s. I can come up with a solution by creating a class that acts as an intermediary between Student and Registrar , showing Student only what he needs to know, but that seems like superfluous. Another solution is to remove from Student any method that needs to access its classes, and place it instead in Registrar or some other class that has access to Registrar .
I ask that I am working on a chess game in Java. I am thinking about the relationship with Piece-Cell and Piece-Player. If it wasn’t normal for Student to have access to Registrar in the above example, here is OK for Piece have access to the Board , since a Piece should look anyway to decide whether any step is valid?
What is the standard practice in such cases?
user64480
source share