开发者

Conditional Logic dynamically

开发者 https://www.devze.com 2023-02-20 21:03 出处:网络
I search for 开发者_开发百科reusable tool or algorithm that enable me to apply conditional logic to questions dynamically.

I search for 开发者_开发百科reusable tool or algorithm that enable me to apply conditional logic to questions dynamically.

lets explain in details:

question 1 - What is your age?

a) less 18        
b)between 18-25     
c) greater than 25

if he choose

a) then he go to Question 2 
b) he will goto question 5 
c) he will goto question 7 

So as I said the next question depends on the answer of the current question.I don't need to set if condition for each question .I need it to be dynamically.

I hope it is clear now . Is there any component or design pattern or algorithm implements what I said

All ideas are welcomed.


It sounds like you probably want a lookup table, effectively mapping the pair (input question, answer) to the next question number to ask. Perhaps it should default to "go to the next question" if there's no entry in the table.

Exactly how you represent it in data structures will depend on what you're using to store the questions. For example, in SQL you could have a table with columns of "input question, answer, next question". In C# you might have a Dictionary<Tuple<int, int>, int>... or possibly (if there aren't going to be huge numbers of questions) just a List<AnswerPath> where AnswerPath contains the same three values as the SQL table would have done. (Change the name, it's awful, but you get the idea.)


Consider building your questions as a graph, where each answer points to an ordered set of other questions.

Even better, give each question a set of prerequisites that must be satisfied by previous answers. For example, for question 2, the prerequisites might be that the answer to question 1 was either a or b.

Then when determining the next question, you would just iterate through each of them until you find the next one whose prerequisites are met.


Add a property called Next Question for every answer.


you could use a Dictionary<Tuple<Question,Answer>>,Question> so that the question and answer became the key for the next question.


Sounds like a job for the Chain of Responsibility pattern...

0

精彩评论

暂无评论...
验证码 换一张
取 消