0Pricing
Clean Architecture & Design Patterns in Practice · درس

Mediator وسلسلة المسؤولية

استكشف نمطين سلوكيين يفصلان المرسلين عن المستلمين: يركّز Mediator الاتصالات مركزيًا، بينما تمرّر Chain of Responsibility الطلبات عبر سلسلة من المعالجات

Mediator وسلسلة المسؤولية درس مجاني في Clean Architecture & Design Patterns in Practice على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.

الأسئلة الشائعة

هل درس «Mediator وسلسلة المسؤولية» مجاني؟

نعم — نص درس «Mediator وسلسلة المسؤولية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Clean Architecture & Design Patterns in Practice، انتقل إلى CoddyKit PRO. تتضمن دورة Clean Architecture & Design Patterns in Practice 4 دروس في المجموع.

ماذا ستتعلم في «Mediator وسلسلة المسؤولية»؟

استكشف نمطين سلوكيين يفصلان المرسلين عن المستلمين: يركّز Mediator الاتصالات مركزيًا، بينما تمرّر Chain of Responsibility الطلبات عبر سلسلة من المعالجات تتمرن على Clean Architecture & Design Patterns in Practice مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Clean Architecture & Design Patterns in Practice؟

لا تُشترط خبرة سابقة. Clean Architecture & Design Patterns in Practice على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «Mediator وسلسلة المسؤولية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Clean Architecture & Design Patterns in Practice هذا؟

نعم. كل درس في Clean Architecture & Design Patterns in Practice يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أنماط Observer وStrategy
  2. أنماط Command وIterator
  3. أنماط Template Method وState
  4. Mediator وسلسلة المسؤولية
← العودة إلى Clean Architecture & Design Patterns in Practice