0Pricing
Clean Architecture & Design Patterns in Practice · 课时

中介者模式与责任链模式

探索两种将发送者与接收者解耦的行为型模式:中介者集中管理通信,责任链则让请求沿处理器链传递

中介者模式与责任链模式 是 CoddyKit 上的免费 Clean Architecture & Design Patterns in Practice 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clean Architecture & Design Patterns in Practice 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「中介者模式与责任链模式」课时是免费的吗?

是的 — 「中介者模式与责任链模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clean Architecture & Design Patterns in Practice 课程的其余内容,请升级到 CoddyKit PRO。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。

「中介者模式与责任链模式」这节课中我会学到什么?

探索两种将发送者与接收者解耦的行为型模式:中介者集中管理通信,责任链则让请求沿处理器链传递 你通过在浏览器中直接运行的动手代码来练习 Clean Architecture & Design Patterns in Practice,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clean Architecture & Design Patterns in Practice 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clean Architecture & Design Patterns in Practice 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「中介者模式与责任链模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clean Architecture & Design Patterns in Practice 课中编写并运行代码吗?

能。每节 Clean Architecture & Design Patterns in Practice 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 观察者与策略模式
  2. 命令与迭代器模式
  3. 模板方法与状态模式
  4. 中介者模式与责任链模式
← 返回 Clean Architecture & Design Patterns in Practice