{"id":1974,"date":"2023-10-27T10:00:05","date_gmt":"2023-10-27T10:00:05","guid":{"rendered":"https:\/\/msgprogramator.testovacka.sk\/java-design-patterns-bridge\/"},"modified":"2025-10-22T11:06:55","modified_gmt":"2025-10-22T11:06:55","slug":"java-bridge","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-bridge\/","title":{"rendered":"Java design pattern Bridge"},"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; Bridge. Design patterns in this category deal with class structure such as <strong>inheritance<\/strong> and <strong>composition<\/strong>.<\/p>\n<p>Read more about 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<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-composite-design-pattern\/\">Composite design pattern<\/a><\/li>\n<\/ul>\n<p><strong>Bridge<\/strong> is a design pattern used in software engineering to separate the abstraction from the implementation so that the two parts can change independently of each other. In doing so, the Bridge pattern uses encapsulation, aggregation, and can use inheritance to divide responsibilities into different classes.<\/p>\n<h2>What problem does the Bridge design pattern solve?<\/h2>\n<p>The Bridge pattern solves the problem when we want a class hierarchy to have two independent dimensions of extensibility &#8211; one for abstraction and one for implementation.<\/p>\n<p>The main goal of the Bridge pattern is to allow clients to select and combine different implementations with different abstractions without changing their class hierarchy. This way we can achieve more flexibility and reusability in the code.<\/p>\n<p>Using the Bridge design pattern allows us to have the abstraction and its implementation defined and extended independently of each other. In addition, it will also allow us to avoid the coupling between the abstraction and its implementation at compile time, so that the implementation can then be selected at runtime.<\/p>\n<h2>Example of Bridge implementation in Java<\/h2>\n<p>Now we will show how to implement the Bridge design pattern in Java. In this example, we will program the ability to shop in different types of stores (e.g. online store, brick-and-mortar store), using different payment methods (e.g. credit card, PayPal). For abstraction we use the abstract class <em>Obchod<\/em> and for different payment implementations we use the interface <em>SposobPlatby<\/em>.<\/p>\n<p>We separate the Abs<em>traction<\/em> from its I<em>mplementation<\/em> by placing them in separate class hierarchies. We then implement the <em>Abstraction<\/em> by delegating to the <em>Implementor<\/em> object.<\/p>\n<p>Abstraction <em>Obchod<\/em> represents different types of shops (e.g. brick-and-mortar shop, online shop).<\/p>\n<p><strong><u>Obchod.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Abstrakcia - typ obchodu\nabstract public class Obchod {\n    protected SposobPlatby sposobPlatby;\n\n    public Obchod(SposobPlatby sposobPlatby) {\n        this.sposobPlatby = sposobPlatby;\n    }\n\n    public abstract void nakup(float suma);\n}\n<\/code><\/pre>\n<p>The more specific abstractions (<em>KamennyObchod<\/em> and <em>InternetovyObchod<\/em>) represent different types of shops that can make purchases and use <em>SposobPlatby<\/em> to process payments.<\/p>\n<p><strong><u>KamennyObchod.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Konkr\u00e9tnej\u0161ia abstrakcia obchodu - kamenn\u00fd obchod\npublic class KamennyObchod extends Obchod {\n    public KamennyObchod(SposobPlatby sposobPlatby) {\n        super(sposobPlatby);\n    }\n\n    public void nakup(float suma) {\n        System.out.println(&quot;N\u00e1kup v kamennom obchode.&quot;);\n        sposobPlatby.spracujPlatbu(suma);\n    }\n}\n<\/code><\/pre>\n<p><strong><u>InternetovyObchod.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Konkr\u00e9tnej\u0161ia abstrakcia obchodu - internetov\u00fd obchod\npublic class InternetovyObchod extends Obchod {\n    public InternetovyObchod(SposobPlatby sposobPlatby) {\n        super(sposobPlatby);\n    }\n\n    public void nakup(float suma) {\n        System.out.println(&quot;N\u00e1kup v internetovom obchode.&quot;);\n        sposobPlatby.spracujPlatbu(suma);\n    }\n}\n<\/code><\/pre>\n<p>The <em>SposobPlatby<\/em> implementer represents different payment methods (e.g. credit card, PayPal).<\/p>\n<p><strong><u>SposobPlatby.java<br \/>\n<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Implement\u00e1tor - rozhranie pre sp\u00f4sob platby\npublic interface SposobPlatby {\n    void spracujPlatbu(float suma);\n}\n<\/code><\/pre>\n<p>Specific implementations (<em>PlatbaKreditnouKartou<\/em> and <em>PlatbaPayPal<\/em>) provide specific implementations of payment methods.<\/p>\n<p><strong><u>PlatbaKreditnouKartou.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Konkr\u00e9tna implement\u00e1cia \u2013 platba kreditnou kartou\npublic class PlatbaKreditnouKartou implements SposobPlatby {\n    private String cisloKarty;\n\n    public PlatbaKreditnouKartou(String cisloKarty) {\n        this.cisloKarty = cisloKarty;\n    }\n\n    public void spracujPlatbu(float suma) {\n        System.out.println(&quot;Spracov\u00e1va sa platba kreditnou kartou vo v\u00fd\u0161ke &quot; + suma + &quot;\u20ac s \u010d\u00edslom karty &quot; + cisloKarty  + &quot;.&quot;);\n    }\n}\n<\/code><\/pre>\n<p>In the <em>Main<\/em> class, we create instances of different types of stores with different payment methods and make purchases. The <strong>Bridge<\/strong> pattern allows us to change and combine payment methods and deal types without changing the hierarchy of the Deal class.<\/p>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.*;\n\npublic class Main {\n    public static void main(String[] args) {\n        SposobPlatby platbaKreditnouKartou = new PlatbaKreditnouKartou(&quot;1234-5678-9012-3456&quot;);\n        SposobPlatby platbaPayPal = new PlatbaPayPal(&quot;mojpaypal@email.com&quot;);\n\n        Obchod internetovyObchod = new InternetovyObchod(platbaKreditnouKartou);\n        Obchod kamennyObchod = new KamennyObchod(platbaPayPal);\n\n        internetovyObchod.nakup(100.0f);\n        kamennyObchod.nakup(50.0f);\n    }\n}\n<\/code><\/pre>\n<p>The output of this example is:<\/p>\n<figure id=\"attachment_1925\" aria-describedby=\"caption-attachment-1925\" style=\"width: 624px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1546\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/10\/Bridge-navrhovy-vzor.webp\" alt=\"java output from the example bridge design pattern\" width=\"624\" height=\"151\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/10\/Bridge-navrhovy-vzor.webp 624w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/10\/Bridge-navrhovy-vzor-300x73.webp 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><figcaption id=\"caption-attachment-1925\" class=\"wp-caption-text\">Bridge design pattern &#8211; example output<\/figcaption><\/figure>\n<h2>Conclusion<\/h2>\n<p>The Bridge design pattern is used in a situation where we need to separate abstraction from implementation so that we can change them independently.<\/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\/10\/Bridge.zip\">Java Bridge code here<\/a>.<\/p>\n<p>Do you know the <a href=\"https:\/\/msgprogramator.sk\/java\/\">Java programming language<\/a>? If you are looking for a job as a <a href=\"https:\/\/msg-life.sk\/en\/jobs\/java-programmer-senior\/\">Java developer<\/a>, see 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 Bridge design pattern and what is it for? Read our new article and download the sample code.<\/p>\n","protected":false},"author":14,"featured_media":1923,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-1974","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\/1974","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=1974"}],"version-history":[{"count":19,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/1974\/revisions"}],"predecessor-version":[{"id":9333,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/1974\/revisions\/9333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/1923"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=1974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=1974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=1974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}