{"id":2369,"date":"2023-09-15T10:20:18","date_gmt":"2023-09-15T10:20:18","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=2369"},"modified":"2025-07-07T10:57:18","modified_gmt":"2025-07-07T10:57:18","slug":"java-adapter","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-adapter\/","title":{"rendered":"Java design pattern Adapter"},"content":{"rendered":"<p>Today we will take a look at the first <strong>Java<\/strong> <strong>design pattern<\/strong> from the <strong>structural pattern<\/strong> category &#8211; Adapter. Design patterns in this category deal with class structure such as <strong>inheritance<\/strong> and <strong>composition<\/strong>.<\/p>\n<p>Read more about other design patterns:<\/p>\n<ul>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-singleton\/\">Singleton design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-builder\/\">Builder design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-prototype\/\">Prototype design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-factory\/\">Factory design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-abstract-factory\/\">Abstract Factory design pattern<\/a><\/li>\n<\/ul>\n<h2>What is the Adapter design pattern?<\/h2>\n<p>Adapter is a design pattern that allows communication between two existing classes or interfaces that cannot communicate with each other due to incompatibility of their interfaces. The adapter allows these incompatible parts to work together by providing a mediated interface.<\/p>\n<h2>What problem does the Adapter design pattern solve?<\/h2>\n<p>So the Adapter solves the problem of communication between incompatible parts of components by creating an intermediate layer between them and passing calls between interfaces. Adapter is useful when we need to integrate existing code or classes with new code that expects a different interface. Instead of changing the code of the existing classes, we can create an adapter that mediates the communication.  <\/p>\n<p>This design pattern can also be found in Java when working with so-called <strong>wrapper <\/strong> classes. Wrapper classes create object types from primitive data types by wrapping them in an adapter, which then implements the desired object type properties. The effect of this is that we can work with an instance of the <strong><em>Integer<\/em><\/strong> class in much the same way as we would work with an ordinary variable of type <strong><em>int<\/em><\/strong>.   <\/p>\n<h2>Example of an Adapter implementation in Java<\/h2>\n<p>Now we will show you how to implement the Adapter pattern in Java.<\/p>\n<p>We will use the Shape interface to calculate the area of any shape.<\/p>\n<p><strong><u>Shape.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic interface Tvar {\n    public int vypocitajObsah(int x);\n}\n<\/code><\/pre>\n<p>This interface is also used by the implementation of the Square class.<\/p>\n<p><strong><u>Square.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Aktu\u00e1lna implement\u00e1cia rozhrania Tvar\npublic class Stvorec implements Tvar {\n    @Override\n    public int vypocitajObsah(int x) {\n        return x * x;\n    }\n}\n<\/code><\/pre>\n<p>We would also like to use an external implementation to calculate the content of the circle provided by a third party, but this does not implement our Shape interface, which we cannot modify because it is already in use.<\/p>\n<p><strong><u>Circle.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Implement\u00e1cia triedy Kruh dodan\u00e1 externe cez kni\u017enicu\npublic class Kruh {\n    public double vypocitajObsahKruhu(int r) {\n        return 3.14 * r * r;\n    }\n}<\/code><\/pre>\n<p>The solution is to adapt the Circle implementation to our Shape interface. So we need an adapter, as the methods are incompatible.<\/p>\n<p><strong><u>CircleAdapter.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class KruhAdapter extends Kruh implements Tvar {\n    @Override\n    public int vypocitajObsah(int x) {\n        return (int) vypocitajObsahKruhu(x);\n    }\n}\n<\/code><\/pre>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<p>Now we can use both the Square and Circle shapes (via the CircleAdapter) and call the area method on them.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.KruhAdapter;\nimport designpatterns.Stvorec;\nimport designpatterns.Tvar;\n\n\/\/ Pou\u017eitie vzoru adapter\npublic class Main {\n    public static void main(String[] args) {\n        Tvar kruh =  new KruhAdapter();\n        System.out.println(&quot;Obsah kruhu: &quot; + kruh.vypocitajObsah(4));\n        Tvar stvorec = new Stvorec();\n        System.out.println(&quot;Obsah \u0161tvorca: &quot; + stvorec.vypocitajObsah(4));\n    }\n}<\/code><\/pre>\n<p>The output of this example is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1353 size-full\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/09\/adapter-design-pattern-vystup.webp\" alt=\"After using the Square and Circle shapes through the CircleAdapter, we use the method to calculate the area.\" width=\"355\" height=\"151\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/09\/adapter-design-pattern-vystup.webp 355w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/09\/adapter-design-pattern-vystup-300x128.webp 300w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>The Adapter design pattern is used in a situation where a class needs to have a different interface to the one it currently has. The Adapter allows classes to work together that would otherwise not work together due to their different interfaces. The Adapter can be divided into a class adapter and an object adapter, depending on the implementation used.  <\/p>\n<p>We have prepared the files with the above example as code that you can run directly in Java. You can download the <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/09\/Adapter.zip\">Java Adapter code here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Adapter design pattern and what is it for? Read our new article and download the sample code. <\/p>\n","protected":false},"author":14,"featured_media":1882,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2369","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\/2369","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=2369"}],"version-history":[{"count":3,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2369\/revisions"}],"predecessor-version":[{"id":5276,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2369\/revisions\/5276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/1882"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}