DEV Community

Cover image for Mediator Design Pattern
Youssef Ghonem
Youssef Ghonem

Posted on • Updated on

Mediator Design Pattern

Take advantage of the mediator design pattern to promote loose coupling and simplify the coding of object interactions, and helps to reduce the number of dependencies among them that you have to manage, the mediator is a central hub through which all interaction must take place, is used to reduce the communication complexity between multiple objects

فايده المدياتور انه بيقلل الاعتماديه بين الاوبجيكتس و بعضها بمعنى اوضح دلوقتي لو حاجه زي المطار مثلا طبعا فيه اكتر من طياره بتنزل ف المطار ده وحصل هبوط مفاجأ مثلا لطياره معينه فلازم تبلغ باقي الطيارات ان محدش ينزل ف المطار دا دلوقتي ف هضطر انها تبلغ كل طياره ب دا وكدا هياخد وقت كتير ف الطبيعي هنا ان هيكون فيه
وسيط هيبلغ باقي الطيارات ان محدش ينزل ف المطار دلوقتي / Mediator

The Mediator object acts as the communication center for all objects. That means when an object needs to communicate to another object, then it does not call the other object directly, instead, it calls the mediator object and it is the responsibility of the mediator object to route the message to the destination object.

A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"

Mediator: It is an interface / abstract and it defines all possible interactions between colleagues.

ConcreteMediator: It is a class that implements the Mediator interface and coordinates communication between colleague objects.

Colleague:the participant type
ده الكلاس اللي هيبدأ يستخدم الميثودز داخل المدياتور

ConcreteColleage1 / ConcreteColleage2: These are classes and implements the Colleague interface. If a concrete colleague (let say ConcreteColleage1) wants to communicate with another concrete colleague (let say ConcreteColleage2),** they will not communicate directly** instead they will communicate via the ConcreteMediator.

هوضح بمثال للمدياتور حاجه زي الفيسبوك مثلا لو عندي جروب اسمه ديزاين باترن و طبعا الجروب فيه ناس كتير و نفترض ان فيه شخص عمل بوست او عمل شير ل لينك معين فلازم باقي الاشخاص اللي فالجروب يتبعتلهم نوتفيكشين او يستقبلو البوست دا

A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"
Mediator
2 هنا عملت انترفيس وحطيت فيه
ميثود واحده علشان اسجل الاشخاص اللي ف الجروب و التانيه دي اللي هبدأ استخدمها علشان ابعت لكل الاشخاص اللي ف الجروب في حال اي شخص نزل بوست مثلا

A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"
Colleague
وده كلاس اليوزر الفنكشنز اللي محتاجها انه يرسل و يستقبل بس ولازم ابعتله ريفرينس للمدياتور لان دا الوسيط الوحيد لباقي اليوزرز

A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"
ConcreteColleague
دا الكلاس اللي بيورث اليوزر علشان اقجر اعمل من وبجيكت واستخدمه في البروجرم بتاعي
اولا بدأت اعمل كونستراكتور علشان ابعت المدياتور والاسم اليوزر
ثانيا عملت امبلمنتيشن لل 2 ميثودز الخاص باليوزر عادي
ولو تلاحظ ف ميثود السيند عملت انجيكت للمدياتورواستدعيت
sendMessage()
علشان يبدأ ينادي على كل اليوزر ويبعتلهم
A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"
ConcreteMediator
دا الامبلمنتيشن بتاع المدياتور 2 ميثودز ببدأ الاول اكريت يوزرز والتانيه ببدأ اخد الرساله وابعتها لباقي اليوزرز
A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"
A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"

code reference : https://github.com/YoussefGhonem/DesignPattern

Please post your feedback, question, or comments about this article
facebook
linkedIn

Top comments (0)