Behavioral Design Pattern

Chain of Responsibility Design Pattern in Java

Pass a request through a sequence of handlers until one handler can process it.

In this lesson: Chain of Responsibility decouples the sender from the receiver by allowing multiple handlers to examine a request in sequence.

Overview

Chain of Responsibility is useful when its recurring design problem appears in a real application. It keeps the relevant responsibilities organized while allowing surrounding code to depend on clear abstractions.

Implementation

abstract class Handler { protected Handler next; Handler next(Handler next) { this.next = next; return next; } abstract void handle(String request); }
final class AuthHandler extends Handler { void handle(String request) { if (request.equals("auth")) System.out.println("Authenticated"); else if (next != null) next.handle(request); } }
new AuthHandler().next(new AuthHandler()).handle("auth");

When should you use it?

Use it for middleware, validation pipelines, approval workflows, logging filters, and request processing stages.

Next step: Continue with the Design Patterns course and explore the next pattern.