{"id":1360,"date":"2023-12-12T14:55:13","date_gmt":"2023-12-12T19:55:13","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1360"},"modified":"2023-12-12T22:56:14","modified_gmt":"2023-12-13T03:56:14","slug":"detailed-explanation-of-mediator-design-pattern","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/detailed-explanation-of-mediator-design-pattern\/","title":{"rendered":"Detailed Explanation of Mediator Design Pattern"},"content":{"rendered":"\n<p>The Mediator Pattern is a behavioral design pattern that defines an object that centralizes communication between a set of objects, allowing them to interact without being directly coupled. It promotes the principle of &#8220;loose coupling&#8221; by encapsulating the way objects interact and communicate, facilitating the reduction of dependencies between them. The Mediator acts as an intermediary, coordinating and controlling the communication flow between objects.<\/p>\n\n\n\n<p>Let&#8217;s explore the details of the Mediator Pattern, covering its intent, structure, implementation considerations, and use cases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intent:<\/h3>\n\n\n\n<p>The primary intent of the Mediator Pattern is to define an object that encapsulates how a set of objects interact, promoting loose coupling and enabling the reusability of individual components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Structure:<\/h3>\n\n\n\n<p>The key components of the Mediator Pattern include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Mediator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Defines an interface for communicating with Colleague objects.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>ConcreteMediator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Implements the Mediator interface and manages the communication between Colleague objects.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Colleague:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Defines an interface for communication with other Colleague objects through the Mediator.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>ConcreteColleague:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Implements the Colleague interface and communicates with other Colleague objects through the Mediator.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Implementation Considerations:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Decoupling Logic:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Mediator encapsulates the communication logic, reducing direct dependencies between Colleague objects.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Centralized Control:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Mediator provides a centralized point of control and coordination for communication between Colleague objects.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Extensibility:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adding new Colleague objects or changing the interaction logic can be done without modifying existing Colleague classes, promoting extensibility.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example Implementation in Python:<\/h3>\n\n\n\n<p>Let&#8217;s consider an example where a Chat Room system uses the Mediator Pattern:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from abc import ABC, abstractmethod\r\n\r\n# Mediator\r\nclass ChatMediator(ABC):\r\n    @abstractmethod\r\n    def send_message(self, message, colleague):\r\n        pass\r\n\r\n# ConcreteMediator\r\nclass ChatRoom(ChatMediator):\r\n    def __init__(self):\r\n        self.colleagues = &#91;]\r\n\r\n    def add_colleague(self, colleague):\r\n        self.colleagues.append(colleague)\r\n\r\n    def send_message(self, message, sender):\r\n        for colleague in self.colleagues:\r\n            if colleague != sender:\r\n                colleague.receive_message(message)\r\n\r\n# Colleague\r\nclass Colleague(ABC):\r\n    def __init__(self, mediator, name):\r\n        self.mediator = mediator\r\n        self.name = name\r\n\r\n    @abstractmethod\r\n    def send(self, message):\r\n        pass\r\n\r\n    @abstractmethod\r\n    def receive_message(self, message):\r\n        pass\r\n\r\n# ConcreteColleague\r\nclass ChatParticipant(Colleague):\r\n    def send(self, message):\r\n        print(f\"{self.name} sends: {message}\")\r\n        self.mediator.send_message(message, self)\r\n\r\n    def receive_message(self, message):\r\n        print(f\"{self.name} receives: {message}\")\r\n\r\n# Usage\r\nmediator = ChatRoom()\r\n\r\nparticipant1 = ChatParticipant(mediator, \"Alice\")\r\nparticipant2 = ChatParticipant(mediator, \"Bob\")\r\n\r\nmediator.add_colleague(participant1)\r\nmediator.add_colleague(participant2)\r\n\r\nparticipant1.send(\"Hello, Bob!\")\r\nparticipant2.send(\"Hi, Alice!\")\r<\/code><\/pre>\n\n\n\n<p>In this example, <code>ChatMediator<\/code> is the Mediator interface, <code>ChatRoom<\/code> is the ConcreteMediator implementing the communication logic, <code>Colleague<\/code> is the Colleague interface, and <code>ChatParticipant<\/code> is a ConcreteColleague. The ChatRoom acts as the mediator, and participants communicate through the mediator.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use Cases:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>GUI Components:<\/strong>\n<ul class=\"wp-block-list\">\n<li>In graphical user interfaces, the Mediator Pattern can be used to manage communication between different components, such as buttons, text fields, and dialogs.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Air Traffic Control:<\/strong>\n<ul class=\"wp-block-list\">\n<li>In air traffic control systems, the Mediator Pattern can be applied to coordinate communication between aircraft, control towers, and other entities.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Online Chat Systems:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Chat rooms and messaging systems often use the Mediator Pattern to handle communication between users.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Stock Trading Systems:<\/strong>\n<ul class=\"wp-block-list\">\n<li>In financial systems, the Mediator Pattern can be employed to coordinate communication between different components involved in stock trading.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Pros and Cons:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Pros:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Decoupling:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Promotes loose coupling between objects, reducing dependencies and enhancing maintainability.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Centralized Control:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Provides a centralized point for managing communication and coordination.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Reusability:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Colleagues can be reused independently of their interaction logic.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Cons:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Introducing a mediator can add complexity, especially for small systems.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Potential Performance Overhead:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Depending on the implementation, there might be a performance overhead associated with the centralized control.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Learning Curve:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Developers need to understand the Mediator Pattern and its implementation to use it effectively.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>The Mediator Pattern is a valuable tool for managing and centralizing communication between objects, promoting loose coupling and enhancing the maintainability of a system. It is particularly useful in scenarios where a set of objects need to interact without directly referencing each other. Understanding the principles and use cases of the Mediator Pattern is essential for effectively applying it in real-world scenarios.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Mediator Pattern is a behavioral design pattern that defines an object that centralizes communication between a set of objects, allowing them to interact without being directly coupled. It promotes the principle of &#8220;loose coupling&#8221; by encapsulating the way objects interact and communicate, facilitating the reduction of dependencies between them. The Mediator acts as an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1440,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[371,203],"tags":[433,468,137],"class_list":["post-1360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-architecture","category-python","tag-design-patterns","tag-mediator-design-pattern","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1360"}],"version-history":[{"count":2,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1360\/revisions"}],"predecessor-version":[{"id":1441,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1360\/revisions\/1441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1440"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}