Chain of responsibility 00:00:00 Make a prediction You will trace a request through handlers and define whether first success, first failure or every handler should terminate the chain. A clerk approves up to twenty dollars and a supervisor up to two hundred. A fifty-dollar refund arrives. Should finance also process it after the supervisor says yes? No in a first-capable-handler chain. The clerk passes fifty onward; the supervisor accepts and stops. Forwarding again could approve the same refund twice. 00:00:33 The mountain of sieves Mountain sieves sit one below another. A large pebble stops in an upper sieve; finer grains travel to the next. If nothing catches the sand and there is no tub at the bottom, it disappears. State what each handler recognizes, whether it forwards afterward, and what an unhandled request means. Ordering can change which handler wins. Chain of responsibility is a behavioral design pattern. Objects that may be able to deal with a request are put in an order. The request goes to the first. Each object either deals with it or passes it on. The sender is free of the receivers. It can be tested alone, and receivers can be added, removed and reordered without touching it. 00:01:21 Route a refund by authority A desk clerk can approve refunds up to twenty dollars. A supervisor can approve up to two hundred. A larger request belongs to finance. A refund of fifty passes the clerk and stops at the supervisor. A refund of five hundred passes both. Record which handler accepted the request. If the chain ends without an authorized handler, return a clear refusal instead of silently claiming approval. A twenty-dollar refund stops at the team lead. Try two hundred dollars and watch it move to the next handler. Now remove the manager. The client needs an explicit outcome for nobody handled it; falling off the end cannot silently count as approval. 00:02:05 Validation may have a different stopping rule An order can pass through address, stock and eligibility validators. Here success may mean every handler ran, while the first failure stops the chain. Contrast that with refund approval, where the first capable handler finishes the work. Define the rule before wiring the handlers. Address validation passes, but stock validation fails. In this chain a success means continue to the next check; in the refund chain a handler's approval meant stop. Encode those two outcomes distinctly or the first successful validator may accidentally approve the entire checkout. A validation pipeline often requires every successful check to continue, whereas an approval chain stops at the first success. Reusing the wrong convention changes the outcome. A short fixed decision with no independent handlers can be expressed more clearly as an ordinary conditional. Express. Middleware functions receive the request, the response and next. The guide says that a function which does not end the cycle must call next, otherwise the request is left hanging. Error-handling middleware is a kind of its own. Django. Middleware is applied top down in the order of the MIDDLEWARE setting, and the documentation compares it with an onion. A layer may short-circuit and return a response: the layers inside never see the request. 00:03:37 Try an interview follow-up Why not just if and else? For three fixed rules, ifs are better. The chain pays when the rules grow, when there are several lists of the same rules, when the order is configured, or when rules are to be tested alone. A twenty-dollar expense reaches a team lead with a fifty-dollar limit. A two-hundred-dollar expense moves to a manager. Trace a third expense that exceeds every limit. Return an explicit pending or rejected result at the end so the caller never mistakes falling off the chain for approval. 00:04:11 What to take with you Put the rules into handlers and the order into one place. Each handler takes the request or passes it on, and the sender knows only the first. Keep that first one: a fluent setNext returns the last. Decide what the end means. In dispatch it is a problem and needs a catch-all or an exception. In a filter it means accepted. In a pipeline it is the endpoint.