00:00

Bridge Design Pattern

What is the Bridge Design Pattern?

The Bridge Design Pattern is a structural design pattern that is used to separate an abstraction from its implementation so that both can change independently.

In simple words, this pattern helps you avoid a tight coupling between high-level logic (abstraction) and low-level details (implementation).

Instead of creating many subclasses for every possible combination, the Bridge pattern connects them using composition.

Why Do We Need the Bridge Pattern?

Without the Bridge pattern, your code can quickly become complex and hard to maintain.

For example:

  • You have different shapes (Circle, Square)
  • You also have different colors (Red, Blue)

If you create a subclass for every combination like RedCircle, BlueCircle, RedSquare, BlueSquare, the number of classes increases rapidly.

The Bridge pattern solves this problem by:

  • Keeping shape logic separate
  • Keeping color logic separate
  • Connecting them using a bridge

Key Components of Bridge Pattern

  • Abstraction – High-level control logic
  • Refined Abstraction – Extended version of abstraction
  • Implementor – Interface for implementation logic
  • Concrete Implementor – Actual implementation

Bridge Design Pattern Example (Java)

Step 1: Create Implementor Interface


interface Color {
    void applyColor();
}

Step 2: Create Concrete Implementations


class RedColor implements Color {
    public void applyColor() {
        System.out.println("Applying red color");
    }
}

class BlueColor implements Color {
    public void applyColor() {
        System.out.println("Applying blue color");
    }
}

Step 3: Create Abstraction


abstract class Shape {
    protected Color color;

    protected Shape(Color color) {
        this.color = color;
    }

    abstract void draw();
}

Step 4: Create Refined Abstraction


class Circle extends Shape {

    public Circle(Color color) {
        super(color);
    }

    public void draw() {
        System.out.print("Drawing Circle with ");
        color.applyColor();
    }
}

Step 5: Test the Bridge Pattern


public class BridgePatternDemo {
    public static void main(String[] args) {

        Shape redCircle = new Circle(new RedColor());
        redCircle.draw();

        Shape blueCircle = new Circle(new BlueColor());
        blueCircle.draw();
    }
}

Output


Drawing Circle with Applying red color
Drawing Circle with Applying blue color

How Bridge Pattern Helps

  • Reduces class explosion
  • Makes code easier to extend
  • Improves maintainability
  • Allows independent changes to abstraction and implementation

When to Use Bridge Pattern

  • When abstraction and implementation should vary independently
  • When you want to avoid too many subclasses
  • When changes in implementation should not affect client code

Conclusion

The Bridge Design Pattern is a powerful way to keep your code clean, flexible, and scalable. By separating what a class does from how it does it, you can easily add new features without breaking existing code.

This makes the Bridge pattern especially useful in large applications where changes are frequent.