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?