CSE Roadmap

Common tier · about 2 hours · 6 parts

Part 2 of 6 · 1 min read

The idea

A request travels until someone handles it.

Think of a customer service line. The first agent handles simple questions. If the question is harder, the agent passes it to a senior agent, and then to a manager. The customer does not choose who answers. The request moves along until somebody can deal with it.

GoF's intent for Chain of Responsibility is to keep a sender from being bound to one receiver. Several objects each get a turn at the request. They are linked into a chain, and the request moves along it until one of them takes care of it.

GoF recommends it in three cases: several objects could handle a request and the right one is unknown beforehand, you want to send a request to one of many objects without naming it, or the set of handlers must be assembled at run time.

Chain of responsibility

The Chain of Responsibility pattern links several handler objects in a line, and passes a request along it until one of them handles the request.

Example: a purchase approval that goes from a team lead to a manager to a director.

Check yourself: 1. Does the customer choose which agent answers? 2. Name one case in which GoF says to use the pattern.