Mediator와 Chain of Responsibility
송신자와 수신자를 분리하는 두 가지 행동 패턴을 살펴보세요. Mediator는 통신을 중앙에서 조정하고 Chain of Responsibility는 요청을 처리기들의 흐름에 따라 전달합니다.
Mediator와 Chain of Responsibility은(는) CoddyKit의 무료 Clean Architecture & Design Patterns in Practice 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Clean Architecture & Design Patterns in Practice 강의 전체를 잠금 해제할 수 있습니다. Clean Architecture & Design Patterns in Practice 강의에는 총 4개의 강의가 포함되어 있습니다.
“Mediator와 Chain of Responsibility”에서 뭘 배우나요?
송신자와 수신자를 분리하는 두 가지 행동 패턴을 살펴보세요. Mediator는 통신을 중앙에서 조정하고 Chain of Responsibility는 요청을 처리기들의 흐름에 따라 전달합니다. 브라우저에서 직접 실행하는 실습 코드로 Clean Architecture & Design Patterns in Practice을(를) 배우며, 24/7 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 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 옵서버와 전략 패턴
- 커맨드와 이터레이터 패턴
- 템플릿 메서드와 상태 패턴
- Mediator와 Chain of Responsibility