Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Chain of Responsibility Pattern

Could somebody provide a simple explanation of the chain of responsibility pattern? I found the wiki article a bit confusing.

like image 723
Dollarslice Avatar asked Feb 08 '12 14:02

Dollarslice


People also ask

Which is considered an example of chain of responsibility pattern?

One of the great example of Chain of Responsibility pattern is ATM Dispense machine. The user enters the amount to be dispensed and the machine dispense amount in terms of defined currency bills such as 50$, 20$, 10$ etc.

What are the consequences of chain of responsibility pattern?

Consequences. Chain of Responsibility has the following benefits and liabilities: Reduced coupling. The pattern frees an object from knowing which other object handles a request.

What is chain of responsibility pattern in Java?

Wikipedia defines Chain of Responsibility as a design pattern consisting of “a source of command objects and a series of processing objects”. Each processing object in the chain is responsible for a certain type of command, and the processing is done, it forwards the command to the next processor in the chain.

What is chain responsibility pattern C#?

Chain of Responsibility is behavioral design pattern that allows passing request along the chain of potential handlers until one of them handles request. The pattern allows multiple objects to handle the request without coupling sender class to the concrete classes of the receivers.


1 Answers

A very good example are java servlet filters - pieces of code that are executed before the HTTP request arrives at its target.

  • the chain contains multiple instances, and each of them performs a different action
  • each instance in the chain can choose to propagate to the next instance, or stop the flow

So, with servlet filters, you can have

  • a filter that checks if the user is authenticated. If he is, the filter propagates to the next filter

  • the next filter checks if the user has permissions to the current resource. If it does, it propagate to the next

  • the next logs the current request URL and the username, and always propagate to the next

  • there is nothing else in the chain, so the target object is finally invoked

like image 125
Bozho Avatar answered Sep 28 '22 12:09

Bozho