In this post, We will talk and learn about the Bridge Design Pattern in Java.
Key Points About Bridge Design Pattern :
- When we have interface hierarchies in 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.
- This design pattern mainly decouples an abstraction from its implementation so that these two can vary independently.
- The implementation of bridge design pattern follows the notion to refer Composition over inheritance.
- We may use the Bridge design patterns when both abstraction and implementation may have different hierarchies independently and we want to hide the implementation from the client application.
Now Let’s move towards the implementation of the Bridge Design Pattern.
Below is the complete source code:
Shape.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kkjavatutorials.bridge; /** * @author KK JavaTutorials *Contract for Shape */ public abstract class Shape { protected Color color; public Shape(Color color) { this.color = color; } public abstract String draw(); } |
Color.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials.bridge; /** * @author KK JavaTutorials *Contract to fill color */ public interface Color { public abstract String fill(); } |
Blue.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kkjavatutorials.bridge.impl; import com.kkjavatutorials.bridge.Color; /** * @author KK JavaTutorials *Concrete implementation of Color */ public class Blue implements Color { @Override public String fill() { return "Color is Blue"; } } |
Red.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kkjavatutorials.bridge.impl; import com.kkjavatutorials.bridge.Color; /** * @author KK JavaTutorials *Concrete implementation of Color */ public class Red implements Color { @Override public String fill() { return "Color is Red"; } } |
Rectangle.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials.bridge.impl; import com.kkjavatutorials.bridge.Color; import com.kkjavatutorials.bridge.Shape; /** * @author KK JavaTutorials *Concrete implementation of Shape */ public class Rectangle extends Shape { public Rectangle(Color color) { super(color); } @Override public String draw() { return "Rectangle drawn " + color.fill(); } } |
Square.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials.bridge.impl; import com.kkjavatutorials.bridge.Color; import com.kkjavatutorials.bridge.Shape; /** * @author KK JavaTutorials * This class establishes bridge between Shape and Color */ public class Square extends Shape { public Square(Color color) { super(color); } @Override public String draw() { return "Square drawn " + color.fill(); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kkjavatutorials.client; import com.kkjavatutorials.bridge.Shape; import com.kkjavatutorials.bridge.impl.Blue; import com.kkjavatutorials.bridge.impl.Rectangle; import com.kkjavatutorials.bridge.impl.Red; import com.kkjavatutorials.bridge.impl.Square; /** * @author KK JavaTutorials *Client program which uses Bridge pattern */ public class ClientTest { public static void main(String[] args) { Shape square = new Square(new Blue()); String blueSquare = square.draw(); System.out.println(blueSquare); System.out.println("============================================"); Shape shape = new Rectangle(new Red()); String redRectangle = shape.draw(); System.out.println(redRectangle); } } |
Output of this Program:
Square drawn Color is Blue
============================================
Rectangle drawn Color is Red
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Flyweight Design Pattern in Java
That’s all about the Bridge Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.