Interactive Q&A With Chain of Responsibility and How to Use It

The imaginary problem

We need to build an interactive Q&A module to be presented to users. When users select an answer, the next question is then displayed based on the answer of previous question, and so on. In other words, depending on the request, different question will be picked by the system.


Here we can imagine the answer from users is a request that is being sent to the system, and the system has to decide which question in the question bank to be displayed next. The system can make the decision by forming a chain of question handlers, where each handler represents one question and makes decision for its corresponding question. If the decision made is not to display the question, the handler passes the request on to the next handler, until the decision to display is made.

public interface IHandler
Handler SetNextHandler(IHandler handler);
object Handle();
public class QuestionHandler
private readonly Question _question;
private QuestionHandler _nextQuestionHandler;
public QuestionHandler(Question question)
_question = question;
public SetNextHandler(QuestionHandler handler)
_nextQuestionHandler = handler;
public Question Handle(string request)
//check condition to load, if the condition is true, then return the question, if not, then pass on to the next handler.
return question;
return _nextQuestionHandler.Handle(request)
//create handler for each question and link the handlers in sequence/chain
var questionHandlers = new List<QuestionHandler>();
foreach(var question in GetAllQuestions())
questionHandlers.Add(new QuestionHandler(question));
for(int i = 0; i < questionHandlers.Count - 1; i++)
//when request comes in, start the chain from the first (usually) handler
var questionToDisplay = questionHandlers[0].Handle(request);

Conclusion and what’s next

This is a fast way to design a Q&A system. However, the performance or speed of system to search for the correct question to display can be the bottleneck. Imagine the question is at the end of a very long chain, the waiting time will be much longer. This can be overcome by improving the data structure and algorithm.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Teng Wei Song

Teng Wei Song

My journey as a self-taught developer. Feel free to drop by my online profile at as well!