Mastering Java Design Patterns - Day 11: Chain of Responsibility Pattern

Mastering Java Design Patterns - Day 11: Chain of Responsibility Pattern

Welcome back, everyone! Today, we delve into the Behavioral Design Patterns, starting with the Chain of Responsibility Pattern. As we transition from Structural to Behavioral patterns, this one offers a fluid handling mechanism for requests, making systems easier to manage and extend.

What is the Chain of Responsibility Pattern?

The Chain of Responsibility Pattern allows a request to be passed along a chain of handlers. Each handler decides either to process the request or to pass it to the next handler in the chain. This pattern is great for decoupling the sender of a request from its receivers.

Why Use the Chain of Responsibility Pattern?

  • Decoupling: It decouples the request's sender from the receivers, allowing multiple objects to have the chance to handle the request.
  • Flexibility: Handlers can be added or changed dynamically without affecting other handlers.
  • Control: It gives more control over the distribution and handling of requests.

How to Implement the Chain of Responsibility Pattern in Java

Here's a simple example of the Chain of Responsibility Pattern:

abstract class Handler {

    protected Handler successor;

    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }

    public abstract void handleRequest(String request);

}        
class ConcreteHandler1 extends Handler {

    public void handleRequest(String request) {

        if (request.equals("Level1")) {
            System.out.println("ConcreteHandler1 handled the request");
        } else {
            if (successor != null) {
                successor.handleRequest(request);
            }
        }
    }
}        
class ConcreteHandler2 extends Handler {

    public void handleRequest(String request) {

        if (request.equals("Level2")) {
            System.out.println("ConcreteHandler2 handled the request");
        } else {
            if (successor != null) {
                successor.handleRequest(request);
            }
        }
    }

}        
class DefaultHandler extends Handler {

    public void handleRequest(String request) {
        System.out.println("DefaultHandler: No handler for " + request);
    }
}        
// Client code

public class ChainDemo {

    public static void main(String[] args) {

        Handler h1 = new ConcreteHandler1();

        Handler h2 = new ConcreteHandler2();

        Handler defaultHandler = new DefaultHandler();

        h1.setSuccessor(h2);

        h2.setSuccessor(defaultHandler);

        h1.handleRequest("Level1");
        h1.handleRequest("Level2");
        h1.handleRequest("Invalid");

    }
}        

Discussion:

Have you implemented the Chain of Responsibility Pattern in your systems? How did it streamline your request handling process? Share your thoughts and experiences in the comments below!

?? Call to Action: If you found this post enlightening, please like, share, and comment! Follow #ehadjistratis for more insights into Java Design Patterns and other tech topics. Let’s continue our collaborative learning journey!

Looking forward to your contributions!

Stay tuned for tomorrow's topic: Command Pattern.

#Java #DesignPatterns #ChainOfResponsibilityPattern #Programming #Coding #SoftwareDevelopment #LearningJourney #JuniorDevelopers #TechCommunity #ehadjistratis

要查看或添加评论,请登录

Emmanuel Hadjistratis (he/him)的更多文章

社区洞察

其他会员也浏览了