{"id":1044,"date":"2021-01-02T09:43:50","date_gmt":"2021-01-02T08:43:50","guid":{"rendered":"http:\/\/mateuszglowinski.pl\/?p=1044"},"modified":"2021-01-03T11:45:06","modified_gmt":"2021-01-03T10:45:06","slug":"mediator-programming-pattern","status":"publish","type":"post","link":"https:\/\/mateuszglowinski.pl\/index.php\/2021\/01\/02\/mediator-programming-pattern\/","title":{"rendered":"Mediator Programming Pattern"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1044\" class=\"elementor elementor-1044\" data-elementor-settings=\"[]\">\n\t\t\t\t\t\t<div class=\"elementor-inner\">\n\t\t\t\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-9ca5f6f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"9ca5f6f\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-26f1cac\" data-id=\"26f1cac\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-10312ab elementor-widget elementor-widget-text-editor\" data-id=\"10312ab\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\"><h2>Introduction<\/h2>\n<div>The mediator pattern allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.<\/div>\n<div><\/div>\n<br \/>\n<div><img loading=\"lazy\" class=\"aligncenter\" src=\"http:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/01\/Mediator-solution.png\" alt=\"\" width=\"491\" height=\"630\" \/><\/div>\n<h2>Definition<\/h2>\nMediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator\u00a0object.\n<br \/><br \/>\n<img loading=\"lazy\" class=\"aligncenter\" src=\"http:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/01\/MediatorProgrammingPatternUML.png\" alt=\"\" width=\"530\" height=\"240\" \/>\n<div>\n<h2>Problem solved by &#8222;Mediator&#8221;<\/h2>\n<div>Imagine we&#8217;re building a cooling system that consists of a power supply, fan and a button. Pressing the button should either turn on or turn off the fan. Before we turn the fan on, we need to turn on the power. Similarly, we have to turn off the power right after the fan is turned off.<\/div>\n<div><\/div>\n<\/div>\n<div>\n\nFirst let&#8217;s implement class for a power supply.\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class PowerSupply {\n    public void turnOn() {\n        System.out.println(\"Turn on power-supply\");\n    }\n\n    public void turnOff() {\n        System.out.println(\"Turn off power-supply\");\n    }\n}\n<\/pre>\n<\/div>\nNow it&#8217;s turn for a fan class. Every electric fan needs source of energy, therefore we need to reference PowerSupply class to Fan class.\n<div>\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Fan {\n    private PowerSupply powerSupply;\n    private boolean isOn = false;\n\n    public Fan(PowerSupply powerSupply){\n        this.powerSupply = powerSupply;\n    }\n\n    public boolean isOn() {\n        return isOn;\n    }\n\n    public void turnOn() {\n        powerSupply.turnOn();\n        isOn = true;\n    }\n\n    public void turnOff() {\n        isOn = false;\n        powerSupply.turnOff();\n    }\n}\n<\/pre>\n<\/div>\nOne last thing to implement is a control for a fan. It might be a button.\n<div>\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Button {\n    private Fan fan;\n\n    public Button(Fan fan)\n    {\n        this.fan = fan;\n    }\n\n    public void press(){\n        if(fan.isOn()){\n            fan.turnOff();\n        } else {\n            fan.turnOn();\n        }\n    }\n}\n<\/pre>\n<\/div>\nLet&#8217;s test our cooling system.\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class CoolingSystem {\n    public static void main(String[] args){\n        PowerSupplier powerSupplier = new PowerSupplier();\n        Fan fan = new Fan(powerSupplier);\n        Button button = new Button(fan);\n\n        System.out.println(fan.isOn());\n        button.press();\n        System.out.println(fan.isOn());\n        button.press();\n        System.out.println(fan.isOn());\n    }\n}\n<\/pre>\nOur system is working, but as you can predict, it will be difficult to implement changes and expand the system in such full of dependencies logic. That is not what good programmers do.\n<h2>Solution<\/h2>\nSolution for reducing element&#8217;s dependencies would be to introduce mediator class. In our case it will also cover cooling system functionality.\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Mediator {\n    private Button button;\n    private Fan fan;\n    private PowerSupply PowerSupply;\n\n    public Mediator() {\n        this.button = new Button(this);\n        this.fan = new Fan(this);\n        this.PowerSupply = new PowerSupply();\n    }\n\n    public void press() {\n        if (fan.isOn()) {\n            fan.turnOff();\n        } else {\n            fan.turnOn();\n        }\n    }\n\n    public void start() {\n        PowerSupply.turnOn();\n    }\n\n    public void stop() {\n        PowerSupply.turnOff();\n    }\n}\n<\/pre>\nComponents should refer to mediator object and not each other, because mediator is taking care about system functionality and dependencies.\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Button {\n    private Mediator mediator;\n\n    public Button(Mediator mediator){\n        this.mediator = mediator;\n    }\n\n    public void press() {\n        mediator.press();\n    }\n}\n<\/pre>\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Fan {\n    private Mediator mediator;\n    private boolean isOn = false;\n\n    public Fan(Mediator mediator){\n        this.mediator = mediator;\n    }\n\n    public boolean isOn() {\n        return isOn;\n    }\n\n    public void turnOn() {\n        mediator.start();\n        isOn = true;\n    }\n\n    public void turnOff() {\n        isOn = false;\n        mediator.stop();\n    }\n}\n<\/pre>\nPowerSupply class does not require any changes. Also notice that Button and Fan classes are referencing back to Mediator class.\n\nWe are ready for retesting our cooling system one more time.\n<pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class CoolingSystem {\n    public static void main(String[] args){\n        Mediator coolingSystem = new Mediator();\n        Button button = new Button(coolingSystem);\n        button.press();\n        button.press();\n    }\n}\n<\/pre>\n<h2>Pros<\/h2>\n<div>\n<ul>\n \t<li><em>Single Responsibility Principle<\/em>. You can extract the\ncommunications between various components into a single place, making it\neasier to comprehend and maintain.<\/li>\n \t<li><em>Open\/Closed Principle<\/em>. You can introduce new mediators without having to change the actual components.<\/li>\n \t<li>You can reduce coupling between various components of a program.<\/li>\n \t<li>You can reuse individual components more easily.<\/li>\n<\/ul>\n<div>\n<h2>Cons<\/h2>\n<div>\n<ul>\n \t<li>Over time a mediator can evolve into a <a href=\"https:\/\/en.wikipedia.org\/wiki\/God_object\">God Object<\/a>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div><\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Introduction The mediator pattern allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[23],"_links":{"self":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1044"}],"collection":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1044"}],"version-history":[{"count":79,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1044\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1044\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}