Design pattern Chain of Responsibility (COR) belongs to the general design concept Divide-and-Conquer.
Imagine that we have a complex business logic with inputs and outputs, and its content is filled with numerous statements.
One of Divide-and-Conquer techniques is to apply COR in the form of a single pipeline and multiple handlers. For example, each handler implements one or multiple statements as a logical unit. The inputs are implemented as requests, and the outputs are implemented as response. The requests are passed through the pipeline and each handler is given a chance to process the requests and add something to responses. In the end, one of the handlers or end of chain handler will return the responses back to the caller.
The real life analogy will be a car assembly line (pipeline). Each worker (handler) only knows how to work on the item on hand, and pass the item down the line to next worker (handler)…
The immediate observations are:
- Each worker (handler) is very good at the item that he/she is assigned to.
- Each worker (handler) really doesn’t care who the worker up the pipeline. They just work on the item passed to them, and make sure to pass the item on hand to next worker (handler)
- Each worker (handler) can be easily to be replaced with another worker (handler)
- The quality of the item that each worker (handler) works on can be easily verified.
- The workflow is efficient and easy to understand and be maintained