Contact Me

Chain of Responsability

 It is a design pattern in which a series of objects are chained together, and each object in the chain has the opportunity to handle a request or pass it on to the next object in the chain. This can be useful when you want to decouple the sender of a request from its receiver, and allow multiple objects to handle the request without the sender needing to know which object will actually handle it.


Here is an example of the chain of responsibility pattern in JavaScript:


class Request {

constructor(requestType, requestContent, requestNumber) {

  this.requestType = requestType;

  this.requestContent = requestContent;

  this.requestNumber = requestNumber;

}

}

class RequestHandler{

constructor(){

  this.successor = null;

}

setSuccessor(successor) {

  this.successor = successor;

}

handleRequest(request) {

  if (this.canHandleRequest(request)) {

   this.processRequest(request);

  }else if (this.successor) {

   this.successor.handleRequest(request);

  }else {

   console.log(`Request of type ${request.requestType} with request number ${request.requestNumber} cannot be handled.`);

  }

}

canHandleRequest(request) {

  return false;

}

processRequest(request) {

// Implementation specific to each handler

}

}

class ConcreteHandler1 extends RequestHandler {

canHandleRequest(request) {

  return request.requestType === 'type1';

}

processRequest(request) {

  console.log(`Request of type ${request.requestType} with request number ${request.requestNumber} is being handled by ConcreteHandler1.`);

}

}

class ConcreteHandler2 extends RequestHandler {

canHandleRequest(request) {

  return request.requestType === 'type2';

}

processRequest(request) {

  console.log(`Request of type ${request.requestType} with request number ${request.requestNumber} is being handled by ConcreteHandler2.`);

}

}

const handler1 = new ConcreteHandler1();

const handler2 = new ConcreteHandler2();

handler1.setSuccessor(handler2);

handler1.handleRequest(new Request('type1', 'Content for request of type 1', 1));

// Output: Request of type type1 with request number 1 is being handled by ConcreteHandler1.

handler1.handleRequest(new Request('type2', 'Content for request of type 2', 2));

// Output: Request of type type2 with request number 2 is being handled by ConcreteHandler2.

handler1.handleRequest(new Request('type3', 'Content for request of type 3', 3));

// Output: Request of type type3 with request number 3 cannot be handled.


In this example, the Request class represents a request that needs to be handled, and the RequestHandler class is an abstract class that defines the structure for handling a request. The ConcreteHandler1 and ConcreteHandler2 classes are concrete implementations of the RequestHandler class that can handle specific types of requests.

To use the chain of responsibility pattern, we first create instances of the concrete handlers and set their successors by calling the setSuccessor() method. Then, when we want to handle a request, we call the handleRequest() method on the first handler in the chain. This method checks if the handler can handle the request using the canHandleRequest() method, and if it can, it processes the request using the processRequest() method. If the handler is not able to handle the request, it passes the request on to the next handler in the chain by calling the handleRequest() method on its successor.

This continues until either the request is handled or there are no more handlers left in the chain. If the request is not able to be handled by any of the handlers in the chain, a message is logged indicating that the request cannot be handled.