O'Reilly logo

Learning PHP Design Patterns by William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Sigmund Freud

We are made wise not by the recollection of our past, but by the responsibility for our future.

George Bernard Shaw

By imposing too great a responsibility, or rather, all responsibility, on yourself, you crush yourself.

Franz Kafka

The buck stops here.

Harry S. Truman

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.

Chain of Responsibility class diagram
Figure 13-1. Chain of Responsibility class diagram ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required