backend 5 min read 2026-01-21

Understanding the Bridge Design Pattern

The Bridge pattern is a structural design pattern that decouples an abstraction from its implementation so that the two can vary independently. This pattern is particularly useful when you want to avoid a permanent binding between an abstraction and its implementation.

What is the Bridge Pattern?

The Bridge pattern uses composition instead of inheritance to separate the abstraction from its implementation. This allows you to change the implementation without affecting the abstraction, and vice versa.

When to Use the Bridge Pattern?

Structure

The Bridge pattern involves:

Example in Java

// Implementor interface
interface DrawingAPI {
    void drawCircle(double x, double y, double radius);
}

// Concrete Implementor 1
class DrawingAPI1 implements DrawingAPI {
    public void drawCircle(double x, double y, double radius) {
        System.out.printf("API1.circle at %.2f:%.2f radius %.2f%n", x, y, radius);
    }
}

// Concrete Implementor 2
class DrawingAPI2 implements DrawingAPI {
    public void drawCircle(double x, double y, double radius) {
        System.out.printf("API2.circle at %.2f:%.2f radius %.2f%n", x, y, radius);
    }
}

// Abstraction
abstract class Shape {
    protected DrawingAPI drawingAPI;
    
    protected Shape(DrawingAPI drawingAPI) {
        this.drawingAPI = drawingAPI;
    }
    
    public abstract void draw();
}

// Refined Abstraction
class Circle extends Shape {
    private double x, y, radius;
    
    public Circle(double x, double y, double radius, DrawingAPI drawingAPI) {
        super(drawingAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    
    public void draw() {
        drawingAPI.drawCircle(x, y, radius);
    }
}

// Usage
public class BridgePatternDemo {
    public static void main(String[] args) {
        Shape circle1 = new Circle(1, 2, 3, new DrawingAPI1());
        Shape circle2 = new Circle(5, 7, 11, new DrawingAPI2());
        
        circle1.draw();
        circle2.draw();
    }
}

Benefits

  1. Decoupling: Separates the interface from the implementation
  2. Flexibility: Allows you to change implementations at runtime
  3. Extensibility: Easy to add new abstractions and implementations independently
  4. Hiding Implementation Details: Clients only see the abstraction interface

Real-World Applications

Conclusion

The Bridge pattern is a powerful tool for creating flexible, maintainable code. By separating abstraction from implementation, you can build systems that are easier to extend and modify without affecting existing code.

← Back to Blog