Advanced Java September 27 ,2025

Chain of Responsibility Design Pattern in Java

1. Introduction

In large systems, requests often need to be processed by multiple handlers. Instead of tightly coupling the request sender to a specific handler, the Chain of Responsibility (CoR) pattern allows a request to pass through a chain of handlers until one of them handles it.

This pattern decouples the sender from the receiver, making the system flexible and easier to extend.

2. Real-Life Analogy

Imagine a customer support system:

  • If the query is simple, the Support Executive handles it.
  • If it’s more complex, it goes to the Team Lead.
  • If still unresolved, it escalates to the Manager.

Each level decides whether it can handle the request or pass it to the next handler.

3. Structure

  • Handler (abstract class or interface) → Defines a method for handling requests and maintains a reference to the next handler.
  • Concrete Handlers → Implement request handling. If unable, forward to the next handler.
  • Client → Initiates the request, unaware of which handler will process it.

4. Custom Java Implementation

Step 1: Create the Handler Interface

abstract class SupportHandler {
    protected SupportHandler nextHandler;

    // Set next handler in chain
    public void setNextHandler(SupportHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // Abstract method to handle request
    public abstract void handleRequest(String issueLevel);
}

Step 2: Concrete Handlers

class SupportExecutive extends SupportHandler {
    @Override
    public void handleRequest(String issueLevel) {
        if (issueLevel.equalsIgnoreCase("Basic")) {
            System.out.println("Support Executive resolved the issue.");
        } else if (nextHandler != null) {
            System.out.println("Support Executive escalates the issue...");
            nextHandler.handleRequest(issueLevel);
        }
    }
}

class TeamLead extends SupportHandler {
    @Override
    public void handleRequest(String issueLevel) {
        if (issueLevel.equalsIgnoreCase("Intermediate")) {
            System.out.println("Team Lead resolved the issue.");
        } else if (nextHandler != null) {
            System.out.println("Team Lead escalates the issue...");
            nextHandler.handleRequest(issueLevel);
        }
    }
}

class Manager extends SupportHandler {
    @Override
    public void handleRequest(String issueLevel) {
        if (issueLevel.equalsIgnoreCase("Critical")) {
            System.out.println("Manager resolved the issue.");
        } else if (nextHandler != null) {
            System.out.println("Manager escalates the issue further...");
            nextHandler.handleRequest(issueLevel);
        } else {
            System.out.println("Issue could not be resolved.");
        }
    }
}

Step 3: Client Code

public class ChainOfResponsibilityDemo {
    public static void main(String[] args) {
        // Create handlers
        SupportHandler executive = new SupportExecutive();
        SupportHandler teamLead = new TeamLead();
        SupportHandler manager = new Manager();

        // Set the chain
        executive.setNextHandler(teamLead);
        teamLead.setNextHandler(manager);

        // Client sends different types of issues
        System.out.println("Case 1: Basic Issue");
        executive.handleRequest("Basic");

        System.out.println("\nCase 2: Intermediate Issue");
        executive.handleRequest("Intermediate");

        System.out.println("\nCase 3: Critical Issue");
        executive.handleRequest("Critical");

        System.out.println("\nCase 4: Unknown Issue");
        executive.handleRequest("Unknown");
    }
}

5. Output

Case 1: Basic Issue
Support Executive resolved the issue.

Case 2: Intermediate Issue
Support Executive escalates the issue...
Team Lead resolved the issue.

Case 3: Critical Issue
Support Executive escalates the issue...
Team Lead escalates the issue...
Manager resolved the issue.

Case 4: Unknown Issue
Support Executive escalates the issue...
Team Lead escalates the issue...
Manager escalates the issue further...
Issue could not be resolved.

6. Benefits

  • Decouples sender and receiver – the client doesn’t care who handles the request.
  • Flexible and extensible – new handlers can be added without changing existing code.
  • Clear separation of responsibilities.

7. When to Use

  • When multiple objects can handle a request.
  • When the handler is not known in advance.
  • When you want to give more than one object a chance to process a request.

 

Next - Command Design Pattern in Java

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com