0Pricing
Clean Architecture & Design Patterns in Practice · Lesson

Mediator and Chain of Responsibility

Explore two behavioral patterns that decouple senders from receivers: Mediator centralizes communication, and Chain of Responsibility passes requests along a line of handlers.

Mediator and Chain of Responsibility is a free Clean Architecture & Design Patterns in Practice lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Clean Architecture & Design Patterns in Practice learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Ways to Decouple Communication

Objects often need to talk to each other, but direct references create tangled webs of dependencies.

This lesson covers two behavioral solutions:

  • Mediator routes all communication through a central hub.
  • Chain of Responsibility passes a request down a line until someone handles it.

The Mediator Problem

Imagine a dialog with buttons, checkboxes, and text fields all reacting to each other. If every widget references every other widget, the coupling explodes.

Mediator replaces this mesh with a star: each widget talks only to the mediator.

Mediator Interface

The mediator defines how components notify it of events.

interface Mediator {
    void notify(Component sender, String event);
}

Concrete Mediator

The concrete mediator contains the coordination logic that used to be scattered.

class Dialog implements Mediator {
    Button submit;
    Checkbox terms;
    public void notify(Component sender, String event) {
        if (sender == terms && event.equals("toggle")) {
            submit.setEnabled(terms.isChecked());
        }
    }
}

Components Stay Dumb

Each component simply reports events to the mediator and reacts to instructions. It does not know about its siblings.

class Checkbox {
    Mediator mediator;
    boolean checked;
    void toggle() {
        checked = !checked;
        mediator.notify(this, "toggle");
    }
    boolean isChecked() { return checked; }
}

When to Use Mediator

  • Components are tightly interconnected.
  • Reuse is hard because objects depend on many others.
  • Behavior is spread across classes and hard to change.

Beware: the mediator itself can grow into a god object if overloaded.

The Chain of Responsibility Problem

Now a different scenario: a request might be handled by one of several processors, and you do not want the sender to know which.

Examples: middleware pipelines, event bubbling, approval workflows.

Handler Interface

Each handler can process a request or pass it to the next handler.

abstract class Handler {
    protected Handler next;
    Handler setNext(Handler n) { this.next = n; return n; }
    abstract void handle(Request r);
}

A Concrete Handler

A handler decides whether it can deal with the request; otherwise it forwards.

class AuthHandler extends Handler {
    void handle(Request r) {
        if (!r.authenticated) {
            System.out.println("Rejected: not authenticated");
            return;
        }
        if (next != null) next.handle(r);
    }
}

Building and Running the Chain

Handlers are linked, then the request enters at the head.

class Request { boolean authenticated = true; }
abstract class H { H next; H link(H n){next=n;return n;} abstract void handle(Request r); }
class Log extends H { void handle(Request r){ System.out.println("logged"); if(next!=null) next.handle(r);} }
class Done extends H { void handle(Request r){ System.out.println("handled"); } }
public class Main {
  public static void main(String[] a){
    H head = new Log();
    head.link(new Done());
    head.handle(new Request());
  }
}

Comparing the Two

  • Mediator: many-to-many coordination through one hub; bidirectional.
  • Chain: a one-directional pipeline; each link is independent and order matters.

Both decouple senders from receivers, but solve different shapes of problem.

Quick Check

Test your understanding of these two patterns.

Recap

You learned two communication-decoupling patterns.

  • Mediator turns a mesh of dependencies into a star around a coordinator.
  • Chain of Responsibility forwards a request along independent handlers until one handles it.

Frequently asked questions

Is the “Mediator and Chain of Responsibility” lesson free?

Yes — the full text of “Mediator and Chain of Responsibility” is free to read here on the web, and the Clean Architecture & Design Patterns in Practice course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Clean Architecture & Design Patterns in Practice course, upgrade to CoddyKit PRO.

What will I learn in “Mediator and Chain of Responsibility”?

Explore two behavioral patterns that decouple senders from receivers: Mediator centralizes communication, and Chain of Responsibility passes requests along a line of handlers. You practise Clean Architecture & Design Patterns in Practice with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Clean Architecture & Design Patterns in Practice?

No prior experience is required. Clean Architecture & Design Patterns in Practice on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mediator and Chain of Responsibility” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Clean Architecture & Design Patterns in Practice lesson?

Yes. Every Clean Architecture & Design Patterns in Practice lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Observer and Strategy Patterns
  2. Command and Iterator Patterns
  3. Template Method and State Patterns
  4. Mediator and Chain of Responsibility
← Back to Clean Architecture & Design Patterns in Practice