Chain of responsibility
medium 45 minA request travels past several handlers until one takes it, or until one stops it.
Watch lesson4:36
Check yourself
One question at a time. Each answer explains why.
Question 1 of 13
What is the main purpose of the pattern?
Who decides whether a request is handled or passed on?
var chain = new A().setNext(new B()).setNext(new C()), and setNext returns its argument. What does chain.handle(x) ask?
Nobody handles the request. What happens?
In a chain with Level1, Level2, Engineering and Triage, Triage is moved to the front. What changes?
A middleware catches errors around next(r) in async code and returns next(r) without await. Effect?
Which of these is a decorator and not a stopping link?
Operators add and remove handlers during the day. Which approach is safe?
The permission service does not answer. The permission link should
First match wins over 5,000 overlapping rules. Which speed-up may change answers?
The team differs only in its approval limit. How do you model four roles?
Average handlers asked with shares 70, 20 and 10 percent for handlers 1, 2 and 3?
Why should a request not be changed by handlers on its way?
Pick an answer to see why.
Not quite. The sender gives the request to the first handler. A tree is Composite, an algorithm chosen before is Strategy, telling many is Observer.
Not quite. Each handler knows its own rule only. A central router would be the list of ifs again.
Not quite. The variable holds the result of the last call: C. A and B are never asked.
Not quite. The pattern itself guarantees nothing. A catch-all or an exception has to be chosen.
Not quite. Triage says yes to everything, so all later handlers are shadowed.
Not quite. Without await inside the try block, the promise is rejected after the block has been left.
Not quite. The timer always delegates. The other three may stop the request.
Not quite. A request reads the reference once and sees either the old or the new chain, never half of both.
Not quite. Security links fail closed. If not, taking down the permission service would open the doors.
Not quite. If two rules overlap, their order decides. The others keep the order.
Not quite. A difference in a number is data. Subclasses are for a difference in behaviour.
Not quite. 0.7 x 1 + 0.2 x 2 + 0.1 x 3 = 1.4.
Not quite. If it must carry results, such as the user, make that a typed and documented field.
Flashcards
Say your answer, then reveal it.
Card 1 of 13
Chain of responsibility in one sentence?
A request goes along a line of handlers until one takes it or stops it, and the sender knows only the first.
Say your answer, then reveal it.
Three flavours?
Dispatch (one takes it), filter (each may refuse), wrapping pipeline (each works before and after).
Say your answer, then reveal it.
What does reaching the end mean?
Dispatch: nobody wanted it. Filter: accepted. Pipeline: the endpoint runs.
Say your answer, then reveal it.
What does the base handler do?
Keeps the next handler and passes on by default, so that subclasses hold only their rule.
Say your answer, then reveal it.
The fluent trap?
setNext returns its argument, so a row of calls gives the last handler.
Say your answer, then reveal it.
The forgotten word?
return before next.handle(request).
Say your answer, then reveal it.
Order rules?
Special first, catch-all last, cheap before expensive, protection before work, errors outermost, effects last.
Say your answer, then reveal it.
When may two links be swapped?
When no request could be taken by both, and neither relies on the other.
Say your answer, then reveal it.
Decorator or chain?
A decorator always delegates. A link may stop.
Say your answer, then reveal it.
Change a chain under load?
Build a new immutable list and replace one reference. Running requests keep their snapshot.
Say your answer, then reveal it.
A link breaks?
Security: fail closed. Comfort: fail open and count.
Say your answer, then reveal it.
Thousands of rules?
Index by a shared condition, keep the old order inside each bucket.
Say your answer, then reveal it.
When a map instead?
When one key decides and keys cannot overlap.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.