Tutorial

Bridge Design Pattern in Java

Published on August 3, 2022
author

Pankaj

Bridge Design Pattern in Java

Today we will look into Bridge Design Pattern in java. When we have interface hierarchies in both interfaces as well as implementations, then bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client programs.

Bridge Design Pattern

Just like Adapter pattern, bridge design pattern is one of the Structural design pattern. According to GoF bridge design pattern is:

Decouple an abstraction from its implementation so that the two can vary independently

The implementation of bridge design pattern follows the notion to prefer Composition over inheritance.

Bridge Design Pattern in Java Example

If we look into bridge design pattern with example, it will be easy to understand. Lets say we have an interface hierarchy in both interfaces and implementations like below image. Bridge Design Pattern Interface Hierarchy Now we will use bridge design pattern to decouple the interfaces from implementation. UML diagram for the classes and interfaces after applying bridge pattern will look like below image. bridge design pattern, bridge pattern uml, bridge pattern example Notice the bridge between Shape and Color interfaces and use of composition in implementing the bridge pattern. Here is the java code for Shape and Color interfaces. Color.java

package com.journaldev.design.bridge;

public interface Color {

	public void applyColor();
}

Shape.java

package com.journaldev.design.bridge;

public abstract class Shape {
	//Composition - implementor
	protected Color color;
	
	//constructor with implementor as input argument
	public Shape(Color c){
		this.color=c;
	}
	
	abstract public void applyColor();
}

We have Triangle and Pentagon implementation classes as below. Triangle.java

package com.journaldev.design.bridge;

public class Triangle extends Shape{

	public Triangle(Color c) {
		super(c);
	}

	@Override
	public void applyColor() {
		System.out.print("Triangle filled with color ");
		color.applyColor();
	} 

}

Pentagon.java

package com.journaldev.design.bridge;

public class Pentagon extends Shape{

	public Pentagon(Color c) {
		super(c);
	}

	@Override
	public void applyColor() {
		System.out.print("Pentagon filled with color ");
		color.applyColor();
	} 

}

Here are the implementation classes for RedColor and GreenColor. RedColor.java

package com.journaldev.design.bridge;

public class RedColor implements Color{

	public void applyColor(){
		System.out.println("red.");
	}
}

GreenColor.java

package com.journaldev.design.bridge;

public class GreenColor implements Color{

	public void applyColor(){
		System.out.println("green.");
	}
}

Lets test our bridge pattern implementation with a test program. BridgePatternTest.java

package com.journaldev.design.test;

import com.journaldev.design.bridge.GreenColor;
import com.journaldev.design.bridge.Pentagon;
import com.journaldev.design.bridge.RedColor;
import com.journaldev.design.bridge.Shape;
import com.journaldev.design.bridge.Triangle;

public class BridgePatternTest {

	public static void main(String[] args) {
		Shape tri = new Triangle(new RedColor());
		tri.applyColor();
		
		Shape pent = new Pentagon(new GreenColor());
		pent.applyColor();
	}

}

Output of above bridge pattern example program is:

Triangle filled with color red.
Pentagon filled with color green.

Bridge design pattern can be used when both abstraction and implementation can have different hierarchies independently and we want to hide the implementation from the client application.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 16, 2019

Keeping applyColor() in both Color interface and Shape abstract class create confusion, both are different method keeping different name would have been good to avoid confusion,

- Sach

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    January 21, 2019

    Do we have any reference in JAVA JDK/API where Bridge pattern.is used ?

    - Sumeet Aggarwal

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 3, 2018

      code is not displayed properly. go to html view of your browser to see the code nevertheless KG

      - lukas

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        May 16, 2017

        If I understood this article correct, first schema addresses the state before, and UML diagram addresses the state after applying Bridge pattern. If that’s what you meant to show, I guess that schema is not correct. Arrows from RedColor and GreenColor towards Triangle and Pentagon, should be pointing vice-versa.

        - Rasa

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          February 23, 2017

          Hi Pankaj, Thanks for the explanation. I am not fully clear on the example. In the example we could create the color instances and pass it to the shapes. Then why can not we call the apply color directly on the color objects themselves? Why we are going thru the shape objects? Thanks in advance,

          - Anupama

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            February 6, 2017

            Great article, I read 2 prior articles which described this pattern, but in no way demonstrated why you would ever have a need to use it. This article does both, and very simply.

            - Paul

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              September 16, 2016

              Please modify UML diagram, you draw association and describing it as composition.

              - Anurag

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 27, 2016

                Please, change builder to bridge in the first line of the article

                - 12

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  March 5, 2016

                  Thanks Pankaj for the wonderful article on Design patterns, but you may want to correct the first line on this “Bridge pattern” page: "When we have interface hierarchies in both interfaces as well as implementations, then builder design " It is supposed to be bridge pattern and not builder.

                  - Jay

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    July 23, 2015

                    Cool explanation as always. Which tool are you using for UML diagram.?

                    - Anil

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.