Skip to main content

Chain of Responsibility Pattern

Structural: Chain of Responsibility


Can you explain the chain of responsibility design pattern?

View Answer:
Interview Response: This behavioral JavaScript design pattern generates a series of receiver objects responding to a request. This approach encourages loose coupling, allowing us to avoid coupling the sender of a request to a receiver and allowing other receivers to handle the request.

The receiving objects get coupled together, and they'll be able to act on the request before passing it over to the following receiver object. It's also simple to add additional recipient objects to the chain.

Code Example: ES6 Implementation





The objects participating in this pattern are:

Client -- example code: Request

  • initiates the request to a chain of handler objects

Handler -- example code: Request.get() method

  • defines an interface for handling the requests
  • implements the successor link (returning 'this')

class Request {
constructor(amount) {
this.amount = amount;
console.log('Request Amount: ' + this.amount);
}

get(bill) {
let count = Math.floor(this.amount / bill);
this.amount -= count * bill;
console.log('Dispense ' + count + ' $' + bill + ' bills');
return this;
}
}

function run() {
let request = new Request(378); //Requesting amount
request.get(100).get(50).get(20).get(10).get(5).get(1);
}

run();

/*

OUTPUT

Request Amount:378
Dispense 3 $100 bills
Dispense 1 $50 bills
Dispense 1 $20 bills
Dispense 0 $10 bills
Dispense 1 $5 bills
Dispense 3 $1 bills

*/
Code Example: ES5 Implementation

let Request = function (amount) {
this.amount = amount;
console.log('Request Amount:' + this.amount);
};

Request.prototype = {
get: function (bill) {
let count = Math.floor(this.amount / bill);
this.amount -= count * bill;
console.log('Dispense ' + count + ' $' + bill + ' bills');
return this;
},
};

function run() {
let request = new Request(378); //Requesting amount
request.get(100).get(50).get(20).get(10).get(5).get(1);
}

run();

/*

OUTPUT

Request Amount:378
Dispense 3 $100 bills
Dispense 1 $50 bills
Dispense 1 $20 bills
Dispense 0 $10 bills
Dispense 1 $5 bills
Dispense 3 $1 bills

*/

In what pattern category does the Chain of Responsibility pattern belong?

View Answer:
Interview Response: The Chain of Responsibility pattern is a behavioral design pattern.

Could you kindly provide an example of a Chain of Responsibility Pattern use case?

View Answer:
Interview Response: You can use it if your program handles various requests differently without knowing beforehand the sequence and type of requests. It allows you to chain several handlers, thus, allowing all of them a chance to process the request.

An illustration of the chain of responsibility pattern is in event bubbling in the DOM. The event propagates through the nested elements, one of which may choose to handle the event.


What are some advantages of employing the Chain of Responsibility design pattern?

View Answer:
Interview Response: Benefits of the Chain of Responsibility Pattern

  • You have control over the sequence in which requests get handled.
  • The principle of single responsibility. Classes that invoke operations get separated from classes that perform tasks.
  • The principle of open/closed. New handlers can get added to the app without disrupting the old client code.


What are the disadvantages of the Chain of Responsibility pattern?

View Answer:
Interview Response: Drawbacks of the Chain of Responsibility Pattern.

  • Some requests may end up unhandled.