Bridge Design Pattern in Java
Introduction
In software development, some abstractions change frequently while their implementations also evolve independently. The Bridge Pattern is a structural design pattern that decouples abstraction from implementation, allowing both to vary independently without affecting each other.
Think of it like a TV remote and TV brands scenario: the remote (abstraction) can control different TV brands (implementation), and both can change independently.
Definition
The Bridge Pattern is a structural design pattern that separates abstraction (high-level control) from implementation (low-level details). It achieves flexibility by composing objects rather than using inheritance.
Key Idea: Favor composition over inheritance to avoid tight coupling and rigid hierarchies.
When to Use the Bridge Pattern
- When you want abstraction and implementation to vary independently.
- When you need multiple implementations of an abstraction.
- When inheritance would create a large number of subclasses, leading to a combinatorial explosion.
Real-World Example
- Remote and TV: A universal remote (abstraction) controls Samsung, LG, or Sony TVs (implementation). Each can change independently.
- Shape and Color: A Shape class can have multiple colors (implementations). Adding a new color doesn’t require modifying the Shape hierarchy.
Java Implementation
Let’s implement a Bridge Pattern for shapes with different colors:
Step 1: Define the Implementor
// Implementor interface
interface Color {
void applyColor();
}
Step 2: Create Concrete Implementors
// Concrete Implementor 1
class Red implements Color {
public void applyColor() {
System.out.println("Applying Red color.");
}
}
// Concrete Implementor 2
class Blue implements Color {
public void applyColor() {
System.out.println("Applying Blue color.");
}
}
Step 3: Define the Abstraction
// Abstraction
abstract class Shape {
protected Color color; // Bridge to Implementor
public Shape(Color color) {
this.color = color;
}
abstract void draw(); // High-level control
}
Step 4: Create Refined Abstractions
// Refined Abstraction 1
class Circle extends Shape {
public Circle(Color color) {
super(color);
}
@Override
void draw() {
System.out.print("Circle filled with ");
color.applyColor();
}
}
// Refined Abstraction 2
class Square extends Shape {
public Square(Color color) {
super(color);
}
@Override
void draw() {
System.out.print("Square filled with ");
color.applyColor();
}
}
Step 5: Test the Bridge Pattern
public class Main {
public static void main(String[] args) {
Shape redCircle = new Circle(new Red());
Shape blueCircle = new Circle(new Blue());
Shape redSquare = new Square(new Red());
Shape blueSquare = new Square(new Blue());
redCircle.draw(); // Circle filled with Red color.
blueCircle.draw(); // Circle filled with Blue color.
redSquare.draw(); // Square filled with Red color.
blueSquare.draw(); // Square filled with Blue color.
}
}
Output:
Circle filled with Applying Red color.
Circle filled with Applying Blue color.
Square filled with Applying Red color.
Square filled with Applying Blue color.
Advantages of the Bridge Pattern
- Decouples Abstraction from Implementation: Both can evolve independently.
- Promotes Composition Over Inheritance: Reduces class explosion.
- Flexibility: Adding new abstractions or implementations is easy without modifying existing code.
- Scalability: Ideal for large systems with multiple variations.
Disadvantages
- Complexity: Adds extra layers and more classes.
- Overhead: Requires careful design to avoid unnecessary indirection.
Summary
The Bridge Pattern is essential when abstraction and implementation need independent evolution. By using composition instead of inheritance, it keeps the system flexible, scalable, and maintainable.
Next, we will explore the Composite Pattern, which is perfect for handling tree-like hierarchical structures and treating individual objects and groups uniformly.