Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other.
Example:
When we purchase a smartphone, we get a charger. The charger cable can be separated so that it can be used as a USB cable to connect other devices. Another example is when we use a remote control to operate a TV. The remote control is the abstraction and the TV is the implementation.
Code:
public abstract class Shape{
protected Color color;
public Shape(Color color){
this.color = color;
}
public abstract void draw();
}
public class Circle extends Shape{
public Circle(Color color){
super(color);
}
public void draw(){
System.out.println("Circle is filled with " + color.fill());
}
}
public class Rectangle extends Shape{
public Rectangle(Color color){
super(color);
}
public void draw(){
System.out.println("Rectangle is filled with " + color.fill());
}
}
public class Triangle extends Shape{
public Triangle(Color color){
super(color);
}
public void draw(){
System.out.println("Triangle is filled with " + color.fill());
}
}
public interface Color{
String fill();
}
public class Red implements Color{
public String fill(){
return "red";
}
}
public class Green implements Color{
public String fill(){
return "green";
}
}
public class Blue implements Color{
public String fill(){
return "blue";
}
}
public class Main{
public static void main(String[] args){
Shape shape1 = new Circle(new Blue());
shape1.draw();
Shape shape2 = new Rectangle(new Green());
shape2.draw();
}
}
Output:
Circle is filled with blue
Rectangle is filled with green
Explain:
The code defines a set of classes that implement the Shape interface. The Shape interface provides definitions for objects that represent some form of geometric shape
Each Shape object provides callbacks to get the bounding box of the geometry, determine whether points or rectangles lie partly or entirely within the interior of the Shape, and retrieve a PathIterator object that describes the trajectory path of the Shape outline
The Color interface provides a fill method that returns a string representing the color of the shape
The Red, Green, and Blue classes implement the Color interface and return strings representing their respective colors
The Circle, Rectangle, and Triangle classes extend the Shape class and implement its draw method. The draw method prints out a message indicating that the shape is filled with a particular color
The Bridge design pattern is used to decouple an abstraction from its implementation so that the two can vary independently. In the context of the code I provided, the Shape class is the abstraction and the Color interface is the implementation. The Bridge pattern allows you to separate the abstraction from the implementation.