Chain of Responsibility Pattern

The Chain of Responsibility pattern: passing a request along a chain of handlers until one deals with it, and ending every chain with an answer for requests nobody accepts.

  • 8 cards
  • 4 questions
  • About 19 min
  • Intermediate to Advanced
Share

A support ticket goes first to a bot, then to an agent, then to an engineer, and the customer who sent it never chooses which. The Chain of Responsibility pattern builds that kind of escalation into code: each handler holds a reference to the next, and either deals with a request or passes it on, so the sender talks only to the first link.

This topic opens on a sender that has to know every possible handler, then reads the participants off the class diagram and follows one request down the chain in a sequence diagram. The template links three handlers in Python, Java and TypeScript and sends requests that stop at different points, including one that no handler accepts.

Two examples follow: expense approval by amount, and web framework middleware in the style of Express, where each link may act and then pass the request on. The gotcha is a request that falls off the end of the chain with nobody noticing, and the exercise builds an email filter whose order decides where a message lands.

By the end you can recognise a chain, build and order one, and say what happens to a request no handler accepts.

Contents

8 cardsnot started