Chain of Responsibility Pattern

Chain of Responsibility (CoR) pattern belongs to the behavioral pattern. As it’s name, the CoR pattern merged with its below and above internode in order to provide it intended outcomes even in the programming world.

In this pattern,

  • We have loosely connected components with each other (That means the sender and receiver does not know from whom do they take the request or response),
  • Sender can pass the object for first component,
  • The sent object is processes by each component sequentially.

That means we can use this pattern when we need to process same request through multiple steps in a sequential manner.

In Java API, the major example for CoR is logger. This logger level implementation in Java API has different handlers like INFO, DEBUG, CRITICAL, WARNING, SEVERE and so on. For an example, if you set log level into “INFO”, it will not print anything below the INFO but above info. See below sample.

The Out put:

You can see that, it prints INFO, WARNING and SEVERE but not FINE. Because FINE is below than INFO and thereby it does not print. That means it goes in a hierarchical level. But, if you do not have particular handler in some level, it just ignore that.

Here, it is the UML diagram for this design pattern.

As you can see, the CoR has handler abstract class or interface which has method to handle the request.

The client is a sender who send request for Handler. Also, to handle the request which is received by Handler has multiple concrete handlers to receive or implement HandleRequest(). Handler has successor as well.

The sequence diagram for CoR Pattern:

  • When the Handler1 receives the HandleRequest, it tries to process it. If it can not processs, then pass to the Handler2.
  • If Handler2 can process it, then it processes and otherwise sent it to the Handler3.
  • Likewise, it goes until the request is handle by the concrete handler even for last handler in sequence.

Now, it is the time to implement the pattern with real world scenario.

|Case Scenario

Let’s say that, we have candidate list who applied and sit for a particular examinations. These, examinations are included with IQ test, General Knowledge (GK) test and Practical exam as well. At the beginning, a candidate is sit for IQ test. If and only if he/she got 30–39 mark is eligible to sit GK test. There candidate should score 40–59 and thereby eligible for a Practical test. During the practical exam he/she should score more than 60 to get the job. Now, after releasing the whole results, the examiners need to analyze the way of candidate who passed these exams (For and example what percentage pass both IQ and GK but not Practical exam, Average marks for IQ test and so on).

⭐️First we need to create a Handler class and in this case it is HandlerChain. It has 2 different implementations. It sets the object for sequence and prints the message when the marks of candidate is not fallen among our handler classes.

⭐️To make the result validation process, I created a class called as Selection which has its constructor to the value and getSelection() to return the selection property.

⭐️Now we are creating a chain which couple handlers with our example. Our handlers are IQPaper, GKPaper and Practicle.

  • You can see in above code snippet, our 3 concrete handler classes implement all the properties and methods on its Parent Handler class.
  • Therefore, all 3 classes have implemented both setNextObject() and processSelection() with its parameters in it’s constructor via super().
  • As well, inside the each Handler class, they implemented their own implementation by based on marking schema.

For an example, in the IQPaper class, processSelection(), it checks if the mark of a candidate is between 30–39. Then, it will display “Candidate is passed Tech test” with the respective marks.

⭐️Now it is time to get an output. So, I created separate objects for each Handler class and set the objects to the next handler. Then finally, we are sending requests to the Handlers to make validations.

The output:

Let’s discuss about the Pros. and Cons. on CoR Design Pattern.

👍Pros.

  • Provide loosely-coupled relation over different component.
  • The chain does not require to know the structure and it just provides references to members.
  • The request can go even for the last handler until the request it handled.

👎Cons.

  • It is hard to debug the runtime errors.
  • If the request does not get a handler, then the request can be dropped.

So, I hope you could get proper understand on the different aspects of Chain of Responsibility Pattern. In my next article, let’s discuss about the Momento Pattern which is used to undo the changes.

Stay Safe !!!

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store