{"id":2217,"date":"2023-11-17T08:49:46","date_gmt":"2023-11-17T08:49:46","guid":{"rendered":"https:\/\/msgprogramator.sk\/java-design-patterns-facade\/"},"modified":"2025-11-06T10:41:50","modified_gmt":"2025-11-06T10:41:50","slug":"java-design-patterns-facade","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-design-patterns-facade\/","title":{"rendered":"Java design pattern Facade"},"content":{"rendered":"<p>Today we will take a look at another <strong>Java<\/strong> <strong>design pattern <\/strong>from the structural <strong>patterns<\/strong> category &#8211; Facade. Design patterns in this category deal with class structure such as <strong>inheritance<\/strong> and <strong>composition<\/strong>.<\/p>\n<h2>What is the Facade design pattern?<\/h2>\n<p><strong>Facade<\/strong> is a design pattern that provides a simplified interface for interacting with a complex system that may consist of multiple classes, interfaces, and components. It is often used in conjunction with the <strong>Singleton<\/strong> design pattern.<\/p>\n<h2>What problem does the Facade design pattern solve?<\/h2>\n<p>The Facade design pattern solves the complexity problem by providing a simple unified interface as an approach to the client that hides the details of the inside of the system, which can be difficult to understand and navigate due to the many classes and interdependencies. The advantage of the Facade is that the complex system can be changed, regardless of the client, who only communicates through the Facade.<\/p>\n<h2>Example of Facade implementation in Java<\/h2>\n<p>Now we will write a program to test the properties of the given numbers. In doing so, we use the <strong>Facade<\/strong> design pattern to hide the complexity of the calculations from the user.<\/p>\n<p>The <strong>SystemA<\/strong> class contains the <em>jePrvocislo<\/em>() method, which determines whether the specified number is a prime number. A prime number is a number greater than 1 that has no deviders other than 1 and itself.<\/p>\n<p><strong><u>SystemA.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class SystemA {\n    public boolean jePrvocislo(int cislo) {\n        if (cislo &lt;= 1) {\n            return false;\n        }\n        for (int i = 2; i &lt;= Math.sqrt(cislo); i++) {\n            if (cislo % i == 0) {\n                return false;\n            }\n        }\n        return true;\n    }\n}\n<\/code><\/pre>\n<p>The <strong>SystemB<\/strong> class contains the <em>jeFibonacihoCislo()<\/em> method, which determines whether the specified number is a member of a Fibonacci sequence. The Fibonacci sequence is such that each number in the sequence is the sum of the two previous numbers (e.g. 0, 1, 1, 2, 3, 5, &#8230;).<\/p>\n<p><strong><u>SystemB.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class SystemB {\n    public boolean jeFibonacihoCislo(int cislo) {\n        int a = 0, b = 1, temp;\n        while (a &lt; cislo) {\n            temp = a;\n            a = b;\n            b = temp + b;\n        }\n        return a == cislo;\n    }\n}<\/code><\/pre>\n<p><strong>SystemC<\/strong> contains the <em>jeOdmocnitelne()<\/em> method, which determines whether the specified number can be square root without a remainder. The square root is the number that multiplied by itself gives the original number.<\/p>\n<p><strong><u>SystemC.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class SystemC {\n    public boolean jeOdmocnitelne(int cislo) {\n        if (cislo &lt; 0) {\n            return false;\n        }\n        int odmocnina = (int) Math.sqrt(cislo);\n        return odmocnina * odmocnina == cislo;\n    }\n}\n<\/code><\/pre>\n<p>The <strong>Facade<\/strong> class combines the previous systems and their methods through the komplexnaOperacia() method, which parses the specified number and returns the result in a text string.<\/p>\n<p><strong><u>Facade.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class Fasada {\n    private SystemA systemA;\n    private SystemB systemB;\n    private SystemC systemC;\n\n    public Fasada() {\n        systemA = new SystemA();\n        systemB = new SystemB();\n        systemC = new SystemC();\n    }\n\n    public String komplexnaOperacia(int cislo) {\n        StringBuilder vysledok = new StringBuilder(&quot;Fas\u00e1da sp\u00fa\u0161\u0165a anal\u00fdzu \u010d\u00edsla &quot; + cislo + &quot;:\\n&quot;);\n        vysledok.append(&quot;Syst\u00e9m A: Je prvo\u010d\u00edslo? &quot;).append(systemA.jePrvocislo(cislo)).append(&quot;\\n&quot;);\n        vysledok.append(&quot;Syst\u00e9m B: Je Fibona\u010diho \u010d\u00edslo? &quot;).append(systemB.jeFibonacihoCislo(cislo)).append(&quot;\\n&quot;);\n        vysledok.append(&quot;Syst\u00e9m C: D\u00e1 sa \u010d\u00edslo odmocni\u0165? &quot;).append(systemC.jeOdmocnitelne(cislo));\n        return vysledok.toString();\n    }\n}\n<\/code><\/pre>\n<p><strong>Main<\/strong> class is the entry point of the program. Creates an instance of the facade and calls its <em>komplexnaOperacia()<\/em> method on any given number. In this example, we set the number to 1. The result of this operation is then printed on the console.<\/p>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.Fasada;\n\npublic class Main {\n    public static void main(String[] args) {\n        Fasada fasada = new Fasada();\n        \/\/ M\u00f4\u017ee\u0161 zmeni\u0165 na ak\u00e9ko\u013evek in\u00e9 \u010d\u00edslo\n        int zadaneCislo = 1;\n        String vysledok = fasada.komplexnaOperacia(zadaneCislo);\n        System.out.println(vysledok);\n    }\n}\n<\/code><\/pre>\n<figure id=\"attachment_1594\" aria-describedby=\"caption-attachment-1594\" style=\"width: 500px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1594\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/11\/priklad-1000-580.webp\" alt=\"The output of this Main.java example\" width=\"500\" height=\"290\" \/><figcaption id=\"caption-attachment-1594\" class=\"wp-caption-text\">The output of this Main.java example<\/figcaption><\/figure>\n<h2>Conclusion<\/h2>\n<p>The Facade design pattern is used when we need to hide complicated and hard to understand code of complex systems. By integrating the Facade between the client and the system, the client can easily interact with the Facade, which ensures that complex operations are called correctly and hides system details.<\/p>\n<p>We have prepared the files with the above example in the form of code that you can run directly in Java. Download the <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/11\/Facade.zip\">Java Facade code here.<\/a><\/p>\n<p>Do you know the <a href=\"https:\/\/msgprogramator.sk\/en\/java\/\">Java programming language<\/a>? If you&#8217;re looking for a job as a <a href=\"https:\/\/msg-life.sk\/en\/jobs\/java-programmer-senior\/\">Java developer<\/a>, check out our <a href=\"https:\/\/msg-life.sk\/en\/benefits\/\">employee benefits<\/a> and respond to our latest <a href=\"https:\/\/msg-life.sk\/en\/jobs\/\">job offers<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Facade design pattern and what is it for? Read our new article and download the sample code.<\/p>\n","protected":false},"author":14,"featured_media":2034,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2217","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\/2217","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=2217"}],"version-history":[{"count":7,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2217\/revisions"}],"predecessor-version":[{"id":9392,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2217\/revisions\/9392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/2034"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}