Fundamentals 12 min read

Chain of Responsibility Design Pattern in Java

The article explains Java’s Chain of Responsibility pattern, describing how abstract and concrete handler classes form a linked chain that passes a request until one processes it, outlines the steps to implement and assemble the chain, and discusses its benefits, drawbacks, and typical applications such as approval workflows and filter pipelines.

DaTaobao Tech
DaTaobao Tech
DaTaobao Tech
Chain of Responsibility Design Pattern in Java

Design patterns improve code reusability, maintainability, and extensibility. This article is the fifth in a series, focusing on the Chain of Responsibility (CoR) pattern, a behavioral pattern.

The core idea of CoR is to treat each handler as a node linked in a chain; a request traverses the chain until a handler processes it, reducing coupling between sender and receiver.

Structure: abstract handler (defines abstract method and next reference), concrete handlers (implement handling logic), and client (assembles the chain).

Usage steps: 1) Create abstract handler class with abstract method and next pointer; 2) Implement concrete handler classes; 3) Create client to link handlers and submit request.

public abstract class AbstractHandler {
    private AbstractHandler next;
    public void setNext(AbstractHandler next) { this.next = next; }
    public AbstractHandler getNext() { return next; }
    public abstract void handleRequest(int leaveDayNum);
}

public class ConcreteHandler1 extends AbstractHandler {
    @Override
    public void handleRequest(int leaveDayNum) {
        if (leaveDayNum <= 1) {
            System.out.println("请假不超过" + leaveDayNum + "天: 学校自动审批通过");
        } else {
            if (getNext() != null) {
                getNext().handleRequest(leaveDayNum);
            } else {
                System.out.println("请假天数过长,需向学院提供签字承诺书");
            }
        }
    }
}
// ... other concrete handlers and client assembling the chain
public class chainOfResponsibility {
    public static void main(String[] args) {
        AbstractHandler handler1 = new ConcreteHandler1();
        AbstractHandler handler2 = new ConcreteHandler2();
        AbstractHandler handler3 = new ConcreteHandler3();
        AbstractHandler handler4 = new ConcreteHandler4();
        handler1.setNext(handler2);
        handler2.setNext(handler3);
        handler3.setNext(handler4);
        handler1.handleRequest(6);
    }
}

The example output shows the request being approved by the dean when the leave days exceed earlier handlers' limits.

Advantages: lowers coupling, enhances extensibility, follows single‑responsibility principle, simplifies connections, and allows dynamic reordering of handlers.

Disadvantages: no guarantee that a request will be handled, performance impact for long chains, and client must correctly configure the chain.

Typical use cases include approval workflows, filter chains in web frameworks (e.g., Servlet filters), and any scenario requiring dynamic processing pipelines.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Chain of ResponsibilityDesign PatternsJavaSoftware Architecturemiddleware
DaTaobao Tech
Written by

DaTaobao Tech

Official account of DaTaobao Technology

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.