Note that there are some explanatory texts on larger screens.

plurals
  1. POOOP: How do I deal with objects that have mutual relations?
    primarykey
    data
    text
    <p>Let's say there are two classes related to each other via some relations. For example, a <code>Student</code> maintains a list of the <code>Class</code>es he takes, and each <code>Class</code> has a list of <code>Student</code>s taking it. Then I am afraid of letting the <code>Student</code> directly being able to modify its set of <code>Class</code>es, because each modification would have to be followed by a similar modification of a <code>Class</code>'s list of <code>Student</code>s, and vice versa.</p> <p>One solution is to have a class whose sole purpose is to keep track of <code>Class</code>-<code>Student</code> relations, say <code>Registrar</code>. But then if some method in <code>Student</code> requires knowledge of its <code>Class</code> list, the <code>Student</code> needs to be passed the <code>Registrar</code>. This seems bad. It seems <code>Student</code> shouldn't have access to the <code>Registrar</code>, where it can also access other <code>Student</code>s. I can think of a solution, creating a class that acts as a mediator between <code>Student</code> and <code>Registrar</code>, showing the <code>Student</code> only what it needs to know, but this seems possibly like overkill. Another solution is to remove from <code>Student</code> any method that needs to access its classes and put it instead in <code>Registrar</code> or some other class that has access to <code>Registrar</code>.</p> <p>The reason I'm asking is that I'm working on a chess game in Java. I'm thinking about the Piece-Cell relations and the Piece-Player relations. If in the above example it wasn't OK for a <code>Student</code> to have access to the <code>Registrar</code>, is it OK here for a <code>Piece</code> to have access to the <code>Board</code>, since a <code>Piece</code> needs to look around anyway to decide if a move is valid?</p> <p>What's the standard practice in such cases?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload