Mediator และ Chain of Responsibility
สำรวจรูปแบบเชิงพฤติกรรมสองแบบที่แยกผู้ส่งออกจากผู้รับ ได้แก่ Mediator รวมศูนย์การสื่อสาร ส่วน Chain of Responsibility ส่งคำขอต่อไปตามลำดับของตัวจัดการ
Mediator และ Chain of Responsibility เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เรียนรู้ Clean Architecture & Design Patterns in Practice ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “Mediator และ Chain of Responsibility” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Mediator และ Chain of Responsibility” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clean Architecture & Design Patterns in Practice ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Mediator และ Chain of Responsibility”
สำรวจรูปแบบเชิงพฤติกรรมสองแบบที่แยกผู้ส่งออกจากผู้รับ ได้แก่ Mediator รวมศูนย์การสื่อสาร ส่วน Chain of Responsibility ส่งคำขอต่อไปตามลำดับของตัวจัดการ คุณปฏิบัติ Clean Architecture & Design Patterns in Practice ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clean Architecture & Design Patterns in Practice หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clean Architecture & Design Patterns in Practice บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “Mediator และ Chain of Responsibility” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clean Architecture & Design Patterns in Practice นี้ได้ไหม
ได้ บทเรียน Clean Architecture & Design Patterns in Practice ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รูปแบบ Observer และ Strategy
- รูปแบบ Command และ Iterator
- รูปแบบ Template Method และ State
- Mediator และ Chain of Responsibility