In the Chain of Responsibility pattern, does the object sending a request need to know in advance which handler will actually process it?
No, and that is the entire point of the pattern. The sender only ever holds a reference to the first handler in the chain and calls its handle method. Each handler in turn decides, purely for itself, whether it can process the request; if it cannot, it passes the request to the next handler it holds a reference to, without needing to know anything about who comes after that. The request effectively travels along the chain until some handler capable of processing it does, and which handler that turns out to be emerges from the chain's structure and each handler's own logic at request time, not from any decision made by the original sender.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response