Question

I have been asked to update an existing "wizard" that presents a linear sequence of questions to users. I was given the task of updating this wizard by referring to a flowchart that was provided by the client. The flowchart departs significantly from the current flow. Given that everything is currently hard-coded with question numbers and valid answers it is going to be very difficult to sort out the changes required.
This made me wonder what sort of technique people would use to model a flowchart/wizard of questions and answers. I've seen reference to using a finite state machine but this does not seem to be a good fit. I'm looking for a technique that would allow for easily moving, inserting, and deleting existing questions without having to sort through a collection of hard-coded references. I've considered using an array that would be loaded via a CSV but am not certain this would be easily maintainable as the list of questions grows.
Note that a pending request is to also allow for jumping into and out of the wizard at any point depending upon the flow of the conversation. To avoid the "question is too broad" response I'm looking for a specific pattern or technique that has been specifically created for this use not a list of possibilities. Thanks!

Was it helpful?

Solution

How come a finite state machine isn't a good fit? See the turnstile example in this wiki about finite-state machine.

Looking at the "state transition table" they have, you can easily think of the column headers as properties of an IQuestion interface:

  • Current State: The current IQuestion
  • Input: IQuestion.Answer.
  • Next State: IQuestion.NextIQuestion
  • Output: Whatever it does to your system.

What you get is a graph of IQuestions (which can be of various types: MultipleChoiceQuestion, DateQuestion, etc.) that have routing logic built in.

You seem to be worried about reuse/reordering of questions, which you can handle with some abstraction. Maybe make IQuestion not care about the routing concerns, and just have properties like QuestionText, PossibleAnswers, etc, then have an IQuestionNode that can store the actual graph/routing concerns. It could handle evaluating the IQuestion and provide a pointer to the next question based on a fixed sequence or by inspecting the selected answer to the current question.

Then all you really need to do is define a State/Event table (see the wiki explanation for more on this) that can be transformed by your system into this graph.

You may also want to take a look at the Strategy Pattern for loading up different graphs of questionnaires based on different needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top