Could somebody provide a simple explanation of the chain of responsibility pattern? I found the wiki article a bit confusing.
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.
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.
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.
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.
A very good example are java servlet filters - pieces of code that are executed before the HTTP request arrives at its target.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With