I have a class Form
, which holds a hash of Question
items.
class Form
{
Dictionary < int , Answer> listOfQuestions;
}
class Question
{
int questionId = 2;
int dependsOnQuestion = 1;
string answer;
public bool IsDependancyMet() {/*problem*/}
}
I want a single Question to depend on an answer of another question, say, if the answer to question 1 is "Married", display question 2, otherwise, don't display it.
What would be the proper way of implementing that?
Would that be adding of a reference of the Form instance to the question instance, to let the later one access to the hash of questions. Though I don't like it.
The other way is to add a method to the Form
class, which receives an question ID and checks whether all it's dependencies are met, but then again, the question should be aware of the form instance.
ON the bottom line: Who should check the dependencies and how, to leave the system modular and each class the less aware of it's environment as possible? Or at least avoid cycle referencing.
Thanks in advance.
What about something along the lines of
class Form
{
Dictionary < int , Answer> listOfQuestions;
int firstQuestion;
public void NextQuestion() {
// ... cycle through `listOfQuestions` looking for a question
// whose dependencies are fullfilled and ask it.
}
}
class Question
{
// ...
public List<int> GetDependencies();
public void Ask();
// ...
}
Form
keeps track of the global 'questioning' state, while individual questions only have knowledge of their direct dependencies. To me, this seems like a relatively clean approach.