Chain of Responsibility

GoF Definition

Avoid coupling the sender of a request to its receiver by giving more than one object a

chance to handle the request. Chain the receiving objects and pass the request along the

chain until an object handles it.

The chain of responsibility pattern allows you to pass a request through a series of handler objects.

Thus a chain of objects is formed wherein one object receives the request, does some specific work, and passes the request to the next object in the chain. At the end of the chain the request is either successfully handled or an exception is generated.

As an example, manufacturing line or assembly line to build a product. It can be anything starting from your simple home appliances to complex machinery like cars/trucks.

As you can see object passes through a series of handlers, and each handler performs a specific task and passes it to the next handler. This is where the chain of responsibility pattern can be used.

Take another example: A mortgage application. Before it gets approved, it has to move all way up in the chain to finally get approval at the last stage. There are various departments/units it will be passed through one after the other and each department is doing some specific checks on your application.

Now let's hope back to software world. In this pattern, you form a chain of objects where each object in the chain can handle a particular kind of request. If an object cannot handle the request fully, it passes the request to the next object in the chain. This process may continue until the end of the chain. This kind of request-handling mechanism gives you the flexibility to add a new processing object (handler) at the end of the chain

Let's take real application: When we are installing a app on our smartphone, we just visit the app store, search for app and hit on "install" button. What happens after that?

Let's assume that your smart phone does 3 things.

  1. Downloading app files,

  2. Extracting/unzipping files and then

  3. Installing it on your smartphone

Below UML shows this design.

The IHandler interface consists of a property, NextHandler , and a method, Process() . The NextHandler property holds a reference to the next handler in the chain. The Process() method contains the processing logic that processes the request in some way. Moreover, Process() is also responsible for continuing the chain by invoking the Process() method on the NextHandler .

All 3 handlers implement IHander interface and each of these handlers have specific job.

DownloadHandler downloads the requested application files(compressed files)

ExtractFileHandler unzips/extracts files and keeps the extracted files in a temporary location.

InstallHandler installs the application on your smart phone.

Each handler implements Process() method as per its responsibility.

Beauty of this design is you can add additional steps like purging all the junk files at the end of installation or/and having a check before starting download to make sure space is available on your smartphone and your smartphone meets hardware or software requirements.

But for now we will keep it simple and stick to just download, extract and install.

Note that, all these handlers are independent of each other from design perspective. Only thing as a programmer we need to do is tie these loosely coupled handlers logically (domain specific) so that process is not halted because of dependencies.

Client code may looks like this:

IHandler handler1 = new DownloadHandler();

IHandler handler2 = new ExtractFileHandler();

IHandler handler3 = new InstallHandler();

handler1.NextHandler = handler2;

handler2.NextHandler = handler3;

handler3.NextHandler = null;

handler1.Process();