0Pricing
Clean Architecture & Design Patterns in Practice · レッスン

MediatorとChain of Responsibility

送信側と受信側を分離する2つの振る舞いパターンを学びます。Mediatorは通信を中央に集約し、Chain of Responsibilityはハンドラーの連鎖に沿ってリクエストを渡します。

「MediatorとChain of Responsibility」はCoddyKit上の無料Clean Architecture & Design Patterns in Practiceレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「MediatorとChain of Responsibility」レッスンは無料ですか?

はい。「MediatorとChain of Responsibility」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clean Architecture & Design Patterns in Practiceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clean Architecture & Design Patterns in Practiceコースには全4レッスンが含まれています。

「MediatorとChain of Responsibility」で何を学びますか?

送信側と受信側を分離する2つの振る舞いパターンを学びます。Mediatorは通信を中央に集約し、Chain of Responsibilityはハンドラーの連鎖に沿ってリクエストを渡します。 ブラウザで直接実行するハンズオンコードでClean Architecture & Design Patterns in Practiceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Clean Architecture & Design Patterns in Practiceを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのClean Architecture & Design Patterns in Practiceは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「MediatorとChain of Responsibility」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このClean Architecture & Design Patterns in Practiceレッスンでコードを書いて実行できますか?

はい。すべてのClean Architecture & Design Patterns in Practiceレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ObserverとStrategyパターン
  2. CommandとIteratorパターン
  3. Template MethodとStateパターン
  4. MediatorとChain of Responsibility
← Clean Architecture & Design Patterns in Practiceに戻る