Chain of responsibility

  • Behavioral
  • Gang of Four
About 2 min


Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.


Real-world example

The Orc King gives loud orders to his army. The closest one to react is the commander, then an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.

In plain words

It helps to build a chain of objects. A request enters from one end and keeps going from an object to another until it finds a suitable handler.

Wikipedia says

In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain.

Programmatic Example

Translating our example with the orcs from above. First, we have the Request class:

public class Request {

  private final RequestType requestType;
  private final String requestDescription;
  private boolean handled;

  public Request(final RequestType requestType, final String requestDescription) {
    this.requestType = Objects.requireNonNull(requestType);
    this.requestDescription = Objects.requireNonNull(requestDescription);

  public String getRequestDescription() { return requestDescription; }

  public RequestType getRequestType() { return requestType; }

  public void markHandled() { this.handled = true; }

  public boolean isHandled() { return this.handled; }

  public String toString() { return getRequestDescription(); }

public enum RequestType {

Next, we show the request handler hierarchy.

public abstract class RequestHandler {
  private final RequestHandler next;

  public RequestHandler(RequestHandler next) { = next;

  public void handleRequest(Request req) {
    if (next != null) {

  protected void printHandling(Request req) {"{} handling request \"{}\"", this, req);

  public abstract String toString();

public class OrcCommander extends RequestHandler {
  public OrcCommander(RequestHandler handler) {

  public void handleRequest(Request req) {
    if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
    } else {

  public String toString() {
    return "Orc commander";

// OrcOfficer and OrcSoldier are defined similarly as OrcCommander

The Orc King gives the orders and forms the chain.

public class OrcKing {

  private List<RequestHandler> handlers;

  public OrcKing() {

  private void buildChain() {
    handlers = Arrays.asList(new OrcCommander(), new OrcOfficer(), new OrcSoldier());

  public void makeRequest(Request req) {
        .filter(handler -> handler.canHandleRequest(req))
        .ifPresent(handler -> handler.handle(req));

The chain of responsibility in action.

var king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));

The console output.

Orc commander handling request "defend castle"
Orc officer handling request "torture prisoner"
Orc soldier handling request "collect tax"

Class diagram

alt text
Chain of Responsibility class diagram


Use Chain of Responsibility when

  • More than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically.
  • You want to issue a request to one of several objects without specifying the receiver explicitly.
  • The set of objects that can handle a request should be specified dynamically.

Known uses