Chapter 13. The Chain of Responsibility Design Pattern
Most people do not really want freedom, because freedom involves responsibility, and most people are frightened of responsibility.
We are made wise not by the recollection of our past, but by the responsibility for our future.
By imposing too great a responsibility, or rather, all responsibility, on yourself, you crush yourself.
The buck stops here.
Passing the Buck
The Chain of Responsibility design pattern separates the sender of a request from the receiver. This avoids coupling of the requester and receiver. Further, the pattern allows a request to be passed along a chain to several different objects that have an opportunity to handle the request. The sender doesn’t need to know which object handles the request, and the object doesn’t need to know who sent the request. There’s no coupling between the two.
The advantages of the Chain of Responsibility is that any object can send the request to the objects that handle it, and the objects that handle a request can be changed so that more or different objects can be incorporated to deal with requests. So changes can be made in both the requester and request handlers without disrupting a larger system. Figure 13-1 shows the class diagram for the Chain of Responsibility pattern.

Figure 13-1. Chain of Responsibility class diagram ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access