Java Tutorial: Chain of Responsibility 🎯

beginner
11 min

Java Tutorial: Chain of Responsibility 🎯

Welcome to the Chain of Responsibility tutorial! In this lesson, we'll dive into one of the behavioral design patterns in Java. By the end of this tutorial, you'll have a solid understanding of the Chain of Responsibility pattern and how to implement it in your projects. Let's get started!

What is Chain of Responsibility? 📝

The Chain of Responsibility is a behavioral design pattern that allows multiple objects to handle a request, each object having a chance to handle the request. It promotes loose coupling by allowing an individual object to pass a request to another object without having specific knowledge about it.

Here's a real-world example: imagine a Help Desk where different technicians handle specific issues. When a customer calls with a problem, the call is first routed to the appropriate technician based on the problem description. The technician responsible for that issue handles it, while the others remain idle. This is a perfect example of the Chain of Responsibility pattern!

Implementing Chain of Responsibility in Java 💡

Let's create a simple implementation of the Chain of Responsibility pattern in Java. We'll create an AbstractHandler class as the base class for our handlers and a ConcreteHandler class to represent each technician at the Help Desk.

java
public abstract class AbstractHandler { protected AbstractHandler successor; public void setSuccessor(AbstractHandler successor) { this.successor = successor; } public abstract void handleRequest(Request request); } public class ConcreteHandler extends AbstractHandler { private final String name; public ConcreteHandler(String name) { this.name = name; } @Override public void handleRequest(Request request) { if (canHandle(request)) { handle(request); } else { if (successor != null) { successor.handleRequest(request); } else { System.out.println("No handler can handle this request."); } } } protected boolean canHandle(Request request) { // Implement the logic to check if this handler can handle the request return false; } protected void handle(Request request) { // Implement the logic to handle the request } }

In the code above, we define the AbstractHandler class as the base class for all our handlers. Each ConcreteHandler instance will represent a technician and handle specific types of requests.

Now, let's create some technicians and test our Chain of Responsibility implementation.

java
public class HelpDesk { public static void main(String[] args) { // Create technicians (handlers) AbstractHandler firstTechnician = new ConcreteHandler("John"); AbstractHandler secondTechnician = new ConcreteHandler("Mike"); AbstractHandler thirdTechnician = new ConcreteHandler("Sarah"); // Set the successor for each technician firstTechnician.setSuccessor(secondTechnician); secondTechnician.setSuccessor(thirdTechnician); // Create a request Request request = new Request("Problem with computer"); // Test the chain of responsibility firstTechnician.handleRequest(request); } } public class Request { private final String description; public Request(String description) { this.description = description; } public String getDescription() { return this.description; } }

In the HelpDesk class, we create three technicians and set their successors. Then, we create a request and test the chain of responsibility by having the first technician handle the request.

Extending the Chain of Responsibility 💡

To extend the Chain of Responsibility, you can create new ConcreteHandler classes for different types of requests and add them to the chain.

Here's an example:

java
public class NetworkHandler extends ConcreteHandler { public NetworkHandler() { super("Network Technician"); } @Override protected boolean canHandle(Request request) { return request.getDescription().contains("network"); || request.getDescription().contains("internet"); } @Override protected void handle(Request request) { System.out.println(getName() + " is handling a network issue: " + request.getDescription()); } }

In the code above, we've created a NetworkHandler class that handles requests related to networks or the internet. You can add this handler to the chain of responsibility before or after the existing technicians, depending on your application's requirements.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the Chain of Responsibility pattern?

That's it for our Chain of Responsibility tutorial! As you've seen, it's a powerful design pattern that promotes loose coupling between objects. Happy coding! 💻🚀