{"id":2987,"date":"2024-01-29T16:00:06","date_gmt":"2024-01-29T16:00:06","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=2987"},"modified":"2025-07-07T10:57:24","modified_gmt":"2025-07-07T10:57:24","slug":"java-mediator","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-mediator\/","title":{"rendered":"Java design pattern Mediator"},"content":{"rendered":"<p>Today we will look at another <strong>Java<\/strong> <strong>design pattern<\/strong> from the category of <strong>behavioural patterns<\/strong> &#8211; Mediator. Design patterns in this category deal with the <strong>interaction<\/strong> between objects and their <strong>responsibility<\/strong>.<\/p>\n<h2>What is the Mediator design pattern?<\/h2>\n<p>The <strong>Mediator<\/strong> design pattern is used to simplify communication between objects in a system by providing a central point for communication between objects. The mediator plays the role of an intermediary and ensures that objects do not communicate directly, which keeps the system easily extensible and maintainable.<\/p>\n<h2>What problem does the Mediator design pattern solve?<\/h2>\n<p>The problem it addresses is that in complex systems, communication between objects can become complicated and unclear. The aim of this pattern is to ensure that objects do not know each other and are therefore not dependent on each other, and that communication between objects does not take place directly, but through a single mediator. This minimizes the binding of objects to each other and makes it easier to change or add new relationships, as all communication goes through a mediator.  <\/p>\n<h2>Example of a Mediator implementation in Java<\/h2>\n<p>We will now demonstrate how the <strong>Mediator <\/strong> design pattern can simplify communication between objects and remove unnecessary dependencies between them by programming a simple chat room that allows participants in a discussion to communicate via both public and private messages.<\/p>\n<p><strong><u>Mediator.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic interface Mediator {\n    void poslatSpravu(Diskuter od, String sprava);\n    void poslatSukromnuSpravu(Diskuter odkoho, Diskuter komu, String sprava);\n}<\/code><\/pre>\n<p>The <strong>Mediator<\/strong> interface defines methods for sending messages to all participants in a discussion and for sending private messages to a specific participant.<\/p>\n<p><strong><u>Chat.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Chat implements Mediator {\n    private List&lt;Diskuter&gt; diskutujuci;\n\n    public Chat() {\n        this.diskutujuci = new ArrayList&lt;&gt;();\n    }\n\n    public void pridat(Diskuter diskuter) {\n        diskutujuci.add(diskuter);\n    }\n\n    @Override\n    public void poslatSpravu(Diskuter od, String sprava) {\n        for(Diskuter prijmatel : diskutujuci) {\n            \/\/ Poslanie spr\u00e1vy v\u0161etk\u00fdm okrem odosielate\u013ea.\n            if(prijmatel != od) {\n                prijmatel.prijatSpravu(sprava);\n            }\n        }\n    }\n\n    @Override\n    public void poslatSukromnuSpravu(Diskuter od, Diskuter komu, String sprava) {\n        for(Diskuter prijmatel : diskutujuci) {\n            \/\/ Poslanie spr\u00e1vy konkr\u00e9tnemu pr\u00edjmate\u013eovi.\n            if(prijmatel == komu) {\n                prijmatel.prijatSukromnuSpravu(od, sprava);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>The <strong>Chat<\/strong> class provides an implementation of the <strong>Mediator<\/strong> interface. It contains a list of discussants and ensures that messages are sent either to all discussants or to specific discussants.<\/p>\n<p><strong><u>Discussant.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic interface Diskuter {\n    void poslatSpravu(String sprava);\n    void prijatSpravu(String sprava);\n    void poslatSukromnuSpravu(Diskuter komu, String sprava);\n    void prijatSukromnuSpravu(Diskuter odkoho, String sprava);\n}<\/code><\/pre>\n<p>The <strong>Discussant<\/strong> interface defines methods for sending and receiving public and private messages.<\/p>\n<p><strong><u>Member.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class Clen implements Diskuter {\n    private String meno;\n    private Chat chat;\n\n    public Clen(String meno, Chat chat) {\n        this.meno = meno;\n        this.chat = chat;\n        chat.pridat(this);\n    }\n\n    @Override\n    public void poslatSpravu(String sprava) {\n        System.out.println(meno + &quot; posiela spr\u00e1vu: &quot; + sprava);\n        chat.poslatSpravu(this, sprava);\n    }\n\n    @Override\n    public void prijatSpravu(String sprava) {\n        System.out.println(meno + &quot; prijal\/a spr\u00e1vu: &quot; + sprava);\n    }\n\n    @Override\n    public void poslatSukromnuSpravu(Diskuter komu, String sprava) {\n        System.out.println(meno + &quot; posiela s\u00fakromn\u00fa spr\u00e1vu: &quot; + sprava);\n        chat.poslatSukromnuSpravu(this, komu, sprava);\n    }\n\n    @Override\n    public void prijatSukromnuSpravu(Diskuter odkoho, String sprava) {\n        System.out.println(meno + &quot; prijal\/a s\u00fakromn\u00fa spr\u00e1vu: &quot; + sprava);\n    }\n}<\/code><\/pre>\n<p>The <strong>Member<\/strong> class implements the <strong>Discussant<\/strong> interface. Each member has a name and a chat to which they are added. They can send messages to everyone, receive messages and send private messages.<\/p>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.Chat;\nimport designpatterns.Clen;\nimport designpatterns.Diskuter;\n\npublic class Main {\n    public static void main(String[] args) {\n        Chat chat = new Chat();\n        Diskuter clen1 = new Clen(&quot;Jozef&quot;, chat);\n        Diskuter clen2 = new Clen(&quot;Petra&quot;, chat);\n        Diskuter clen3 = new Clen(&quot;Dominik&quot;, chat);\n        Diskuter clen4 = new Clen(&quot;Karolina&quot;, chat);\n        clen1.poslatSpravu(&quot;Ahoj v\u0161etci!&quot;);\n        clen4.poslatSukromnuSpravu(clen1, &quot;Ahoj Jozef!&quot;);\n        clen3.poslatSpravu(&quot;Idete si zahra\u0165 stoln\u00fd futbal?&quot;);\n        clen1.poslatSpravu(&quot;Jasn\u00e9, pre\u010do nie.&quot;);\n        clen2.poslatSpravu(&quot;M\u00e1m pr\u00e1cu, ale jednu si zahr\u00e1m.&quot;);\n        clen4.poslatSpravu(&quot;Ja sa prid\u00e1m.&quot;);\n        clen3.poslatSpravu(&quot;Za min\u00fatku dole.&quot;);\n        clen2.poslatSukromnuSpravu(clen1, &quot;Vyklepeme ich ako rezne :-D&quot;);\n    }\n}<\/code><\/pre>\n<p>The <strong>Main <\/strong> class creates a chat object and adds some chat members to it. It then demonstrates the functionality of chat &#8211; communication between members, such as sending and receiving messages, including private messages. <\/p>\n<p>There are no fixed links between the objects (members of the discussion), all communication takes place through an intermediary &#8211; the chat. Adding new members to the discussion is just as easy and doesn&#8217;t increase the complexity of communication. <\/p>\n<p>The output of this example is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2639 size-full\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vystup-z-prikladu-mediator-410-550.webp\" alt=\"There are no fixed links between the objects (members of the discussion), all communication takes place through an intermediary - the chat. \" width=\"410\" height=\"550\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vystup-z-prikladu-mediator-410-550.webp 410w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vystup-z-prikladu-mediator-410-550-224x300.webp 224w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>The <strong>Mediator <\/strong> design pattern is often used when we want to simplify communication and messaging between objects by centralizing communication. The complexity of a system increases with the number of dependencies and relationships between objects; this design pattern helps to significantly reduce them. <\/p>\n<p>We have prepared the files with the above example in the form of code that you can run directly in Java. You can download the <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/Mediator.zip\">Java <strong>Mediator <\/strong> code here<\/a>. <\/p>\n<p>If you&#8217;re a <a href=\"https:\/\/msg-life.sk\/en\/jobs\/java-programmer-senior\/\">Java developer<\/a> looking for work, check out our <a href=\"https:\/\/msg-life.sk\/en\/benefits\/\">employee benefits<\/a> and respond to our <a href=\"https:\/\/msg-life.sk\/en\/jobs\/\">job offers<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Java Mediator design pattern and what is it for? Read our new article and download the sample code.<\/p>\n","protected":false},"author":14,"featured_media":2643,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2987","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/comments?post=2987"}],"version-history":[{"count":4,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2987\/revisions"}],"predecessor-version":[{"id":5272,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2987\/revisions\/5272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/2643"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}