Chain of responsibility design pattern in java

In this article, We are going to discuss widely used behavioral design patterns such as chain of responsibility design pattern in java. Last time we have discussed adapter design patterns.

Chain of responsibility design pattern in Java:

Chain of responsibility is a design pattern where a sender sends a request to a chain of objects, where the objects in the chain decide themselves who to honor the request. If an object in the chain decides not to serve the request, it forwards the request to the next object in the chain. This design pattern basically used to achieve loose coupling.

Let me explain in the real-time scenario, It is similar to ‘passing the question in a quiz scenario’. When the quiz master asks a question to a person, if he doesn’t know the answer, he passes the question to the next person and so on. When one person answers the question, the passing flow stops. Sometimes, the passing might reach the last person and still, nobody gives the answer.

What problems can the Chain of Responsibility design pattern solve?

  • Coupling the sender of a request to its receiver should be avoided.
  • It should be possible that more than one receiver can handle a request.

Some of the Chain of Responsibility design example in java:

  • javax.servlet.Filter#doFilter()
  • java.util.logging.Logger#log()
  • try catch block design.

Example:

In this example. We are going to print each level of the message to its level and print accordingly otherwise does not print and pass the message to its next logger.

For the First level, we are printing only in the first level but for the second level, we are going to print both first and second level. Similar to this one for the third level we are going to print all the level.

Step 1: AbstractLevelMessenger.java

Create an abstract level messenger class as below shown.

Step 2: LevelType.java

Create an enum class for defining the constant level.

Step 3: FirstLevel.java

Create the first level message printer class as below.

Step 4: SecondLevel.java

Create the Second level message printer class as below.

Step 5: ThirdLevel.java

Create the Third level message printer class as below.

Step 6: AppDemo.java

Create different types of level messenger object and assign them levels and set next level in each messenger. Next level in each messenger represents the part of the chain.

output :

Chain of responsibility design pattern in Java
Chain of responsibility design pattern in Java

When to use the Chain of Responsibility Design Pattern in java:

  • When more than one objects may handle a request, and the handler isn’t known a priority. The handler should be ascertained automatically.
  • You want to issue a request to one of several objects without specifying the receiver explicitly.
  • The set of objects that can handle a request should be specified dynamically.

Related Articles:

 

Happy Learning !!!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons