I'm facing some object relationship problems in an application that, among other things, handles multiple choices exercises.
Thinking of those exercises, I can that:
1- I have several exercises, that have several questions that have several alternatives. 2- A specific question can be part of several exercises. 3- A specific alternative can NOT be part of several questions.
In terms of relationship:
Exercise < n-n > Question
Question < 1-n > Alternative
I coded the classes the way bellow:
class Exercise {
private $id;
private $name;
private $questions = []; //array of ExerciseQuestion
//...
}
class ExerciseQuestion {
private $id;
private $exercise = new Exercise();
private $question = new Question();
private $order; //handles question's order of appearance
//...
}
class Question {
private $id;
private $wording;
//...
}
class Alternative {
private $id;
private $question = new Question();
private $text;
//...
}
I don't know if I did it wright, but, instead of storing relationships ID's in classes attributes, I'm storing an instance of the related class. It seemed to be right approach when I started.
The problem I foresee is that, in the relation Exercise-ExerciseQuestion-Question, I will be thrown in a hell of circular references. I will have an Exercise that has several ExerciseQuestion that holds an instance of Exercise with a lot of ExerciseQuestion inside it. Am I right in this assumption?
Is there a better - or correct - way to express that kind of relationship? Should I change to store ID's instead of instances or can can make use of some design pattern?
If I'm reading you correctly, you don't actually have a problem. If your domain includes circular or self-referencing relationships, then your models should too.
Since your objects are entities (they have identities), you'll want to take care that only one instance exists for each id. But as long as you enforce that, they can refer to each other however you need them to.
Now, circular references can be tricky to deal with if you need to traverse the object graph in some exhaustive way. In that case, you'll need to take care to not get yourself into an endless loop. But if your domain actually has these relationships, then you need to model them, and you'll have to implement some way to detect cycles and deal with them. The details will be informed by your particular needs.