The Bridge design pattern is a structural pattern that decouples an abstraction from its implementation, allowing the two to vary independently. In other words, it separates the interface from its implementation, allowing them to evolve independently without affecting each other. In this article, we will discuss the Bridge pattern in detail, including its implementation and practical use cases.
Implementation of Bridge Pattern
The Bridge pattern consists of two main components: the Abstraction and the Implementor.
Abstraction: This is the interface that defines the abstraction or high-level logic that the client uses.
Implementor: This is the interface that defines the implementation or low-level logic that the Abstraction uses.
The Abstraction and the Implementor are connected by a bridge that allows the two to work together without being tightly coupled.
Here’s an example implementation of the Bridge pattern in Java:
Define the Abstraction interface:
public interface Shape {
public void draw();
}
Define the Implementor interface:
public interface Color {
public void fill();
}
Create concrete implementations of the Abstraction interface:
public class Circle implements Shape {
private Color color;
public Circle(Color color) {
this.color = color;
}
public void draw() {
System.out.println("Drawing Circle in " + color.fill() + " color");
}
}
public class Rectangle implements Shape {
private Color color;
public Rectangle(Color color) {
this.color = color;
}
public void draw() {
System.out.println("Drawing Rectangle in " + color.fill() + " color");
}
}
Create concrete implementations of the Implementor interface:
public class Red implements Color {
public void fill() {
return "Red";
}
}
public class Green implements Color {
public void fill() {
return "Green";
}
}
Use the Abstraction interface to draw shapes in different colors:
public static void main(String[] args) {
Shape circle = new Circle(new Red());
circle.draw();
Shape rectangle = new Rectangle(new Green());
rectangle.draw();
}
In this example, we define the Abstraction interface Shape that specifies a method for drawing shapes. We also define the Implementor interface Color that specifies a method for filling colors. We then create concrete implementations of the Abstraction interface (Circle and Rectangle) that use an instance of the Implementor interface (Color) to provide the required functionality. Finally, we use the Abstraction interface to draw shapes in different colors.
Practical Use Cases of Bridge Pattern
The Bridge pattern is commonly used in Java applications to:
Separate abstraction from implementation: The Bridge pattern is useful when we want to separate the abstraction from its implementation. This allows us to change the implementation without affecting the Abstraction interface.
Reduce coupling: By separating the Abstraction from its implementation, the Bridge pattern reduces the coupling between the two, making the code more flexible and easier to maintain.
Provide multiple implementations: The Bridge pattern allows us to provide multiple implementations of the same Abstraction interface, making it easier to add new features or modify existing ones.
Implement platform-independent code: The Bridge pattern is often used to create platform-independent code, allowing the same code to be used across different platforms without modification.
Conclusion
The Bridge pattern is a useful pattern that allows us to separate the abstraction from its implementation. It provides a bridge between the two, allowing them to work together without being tightly coupled. The Bridge pattern is widely used in Java applications, particularly in cases where we want to separate the implementation from the abstraction, reduce coupling, provide multiple implementations, or implement platform-independent code.