The Chain of Responsibility pattern
The Chain of Responsibility pattern is a GoF behavioral pattern that chains classes to handle complex scenarios efficiently, with limited effort. Once again, the goal is to take a complex problem and break it into multiple smaller units.
Goal
The Chain of Responsibility pattern aims to chain multiple handlers that each solve a limited number of problems. A handler decides to process the request or pass it on to the next handler in the chain.
We often create a default handler that executes logic at the end of the chain as the terminal handler. Such a handler can throw an exception (for example, OperationNotHandledException
) to cascade the issue up the call stack to a consumer who knows how to handle and react to it. Another strategy is creating a terminal handler that does the opposite and ensures nothing happens.
Design
The most basic Chain of Responsibility starts by defining an interface that handles a request...