Advanced Java September 20 ,2025

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

  1. Decouples Abstraction from Implementation: Both can evolve independently.
  2. Promotes Composition Over Inheritance: Reduces class explosion.
  3. Flexibility: Adding new abstractions or implementations is easy without modifying existing code.
  4. Scalability: Ideal for large systems with multiple variations.

Disadvantages

  1. Complexity: Adds extra layers and more classes.
  2. 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.

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com