{"id":2374,"date":"2023-12-15T10:10:47","date_gmt":"2023-12-15T10:10:47","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=2374"},"modified":"2025-10-22T11:05:54","modified_gmt":"2025-10-22T11:05:54","slug":"java-proxy","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-proxy\/","title":{"rendered":"Java design pattern Proxy"},"content":{"rendered":"<p>Today we will take a look at another <strong>Java <\/strong><strong>design pattern<\/strong> from the <strong>structural patterns<\/strong> category &#8211; Proxy. Design patterns in this category deal with class structure such as <strong>inheritance<\/strong> and <strong>composition<\/strong>.<\/p>\n<h2>What is the Proxy design pattern?<\/h2>\n<p>The <strong>Proxy<\/strong> pattern is a design pattern that allows you to control access to an object by making it available through another object (a proxy) and provide additional functionality such as access rights authentication, access logging, delayed creation of challenging objects, and more. This pattern belongs to the group of structural patterns because it deals with how objects are composed into larger structures.<\/p>\n<h2>What problem does the Proxy design pattern solve?<\/h2>\n<p>Proxy effectively solves cases where we want to have control over access to an object. For example, if we are working with an object with sensitive data that we would like to let the application access only in certain situations, or if we need to know which clients have accessed the object and when. Proxy gives us the ability to handle such situations without direct access to the main object. For the client, using a proxy object is similar to using a real object because they both implement the same interface, although in reality the proxy is a <strong>wrapper<\/strong> class of the real object.<\/p>\n<h2>Example of Proxy implementation in Java<\/h2>\n<p>We will now implement a program that demonstrates the use of the <strong>Proxy<\/strong> design pattern in Java when logging into an information system. The program checks access rights and logs access.<\/p>\n<p><strong><u>Pristup.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Rozhranie definuj\u00face spolo\u010dn\u00e9 met\u00f3dy pre RealnyPristup a ProxyPristup.\npublic interface Pristup {\n    void vykonajOperaciu(String meno, String heslo, int idKlienta);\n}<\/code><\/pre>\n<p>The <strong> Pristup <\/strong> interface defines a <em>vykonajOperaciu<\/em> method common to RealnyPristup a ProxyPristup.<\/p>\n<p><strong><u>RealnyPristup.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ RealnyPristup - skuto\u010dn\u00fd objekt, ktor\u00fd implementuje rozhranie Pristup.\npublic class RealnyPristup implements Pristup {\n    public void vykonajOperaciu(String meno, String heslo, int idKlienta) {\n        System.out.println(&quot;Vykon\u00e1vam oper\u00e1ciu pre klienta s ID: &quot; + idKlienta);\n    }\n}<\/code><\/pre>\n<p>The <strong>RealnyPristup<\/strong> class implements the <strong>Pristup<\/strong> interface. It represents a real object that also performs a real operation for the client with the given ID.<\/p>\n<p><strong><u>ProxyPristup.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\nimport java.util.Date;\n\npublic class ProxyPristup  implements Pristup {\n    private RealnyPristup realnyPristup;\n\n    public void vykonajOperaciu(String meno, String heslo, int idKlienta) {\n        logujPristup(idKlienta);\n        if (skontrolujPristupovePrava(meno, heslo)) {\n            if (realnyPristup == null) {\n                realnyPristup = new RealnyPristup();\n                System.out.println(&quot;Pr\u00edstup bol povolen\u00fd.&quot;);\n            }\n            realnyPristup.vykonajOperaciu(meno, heslo, idKlienta);\n        } else {\n            System.out.println(&quot;Nespr\u00e1vne pr\u00edstupov\u00e9 \u00fadaje. Pr\u00edstup bol zamietnut\u00fd!&quot;);\n        }\n    }\n\n    private boolean skontrolujPristupovePrava(String meno, String heslo) {\n        \/\/ Simul\u00e1cia kontrolu pr\u00edstupov\u00fdch pr\u00e1v, m\u00f4\u017eete prida\u0165 vlastn\u00fa logiku\n        return &quot;admin&quot;.equals(meno) &amp;&amp; &quot;heslo123&quot;.equals(heslo);\n    }\n\n    private void logujPristup(int idKlienta) {\n        \/\/ Simul\u00e1cia logovania pr\u00edstupu\n        System.out.println(&quot;Pristup klienta s ID [&quot; + idKlienta + &quot;] do syst\u00e9mu o &quot; + new Date());\n    }\n}<\/code><\/pre>\n<p>The <strong>ProxyPristup<\/strong> class implements the <strong>Pristup<\/strong> interface. It represents a proxy object that controls access rights based on a username and password. It also logs the access and, if the access data is correct, delegates the call of the <em>vykonajOperaciu <\/em> method to the real object <strong>RealnyPristup<\/strong>.<\/p>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.Pristup;\nimport designpatterns.ProxyPristup;\n\npublic class Main {\n    public static void main(String[] args) throws InterruptedException {\n        \/\/ Pou\u017eitie Proxy\n        Pristup proxy = new ProxyPristup();\n        proxy.vykonajOperaciu(&quot;jwagner&quot;, &quot;heslo123&quot;, 823);\n        System.out.println();\n        Thread.sleep(800);\n        proxy.vykonajOperaciu(&quot;admin&quot;, &quot;heslo1234&quot;, 111);\n        System.out.println();\n        Thread.sleep(700);\n        proxy.vykonajOperaciu(&quot;admin&quot;, &quot;heslo123&quot;, 111);\n    }\n}<\/code><\/pre>\n<p>An instance of the <strong>ProxyPristup<\/strong> class is created in the main method and we test how our implementation checks and logs client accesses.<\/p>\n<p>The program prints access and access denial messages based on the access data entered. If the credentials match the expected values (username &#8220;admin&#8221; and password &#8220;password123&#8221;), Proxy will allow access to the real object and execute its operation. The access time is also logged.<\/p>\n<p>The output of this example is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2227 size-full\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/12\/vystup-prikladu-1000-460.webp\" alt=\"An instance of the ProxyPristup class is created in the main method\" width=\"1000\" height=\"460\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/12\/vystup-prikladu-1000-460.webp 1000w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/12\/vystup-prikladu-1000-460-300x138.webp 300w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/12\/vystup-prikladu-1000-460-768x353.webp 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>The Proxy design pattern is often used in situations where we need to provide access control and first verify the correctness of the data and\/or check the permissions to use the methods before calling the methods on the real object.<\/p>\n<p>We have prepared the files with the above example in the form of code that you can run directly in Java. <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/12\/Proxy.zip\">Download the Java Proxy code here<\/a>.<\/p>\n<p>Do you know the <a href=\"https:\/\/msgprogramator.sk\/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 the Proxy design pattern and what is it for? Read our new article and download the sample code.<\/p>\n","protected":false},"author":14,"featured_media":2224,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2374","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\/2374","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=2374"}],"version-history":[{"count":5,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2374\/revisions"}],"predecessor-version":[{"id":9331,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2374\/revisions\/9331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/2224"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}