bridge design pattern

what is bridge design pattern and it’s advantage in java

what is bridge design pattern?

The Bridge Pattern is a structural design pattern that separates abstraction from implementation, allowing them to vary independently. It involves creating a bridge interface that contains a reference to the implementation interface. This way, changes in the abstraction won’t affect the client code, and changes in the implementation won’t affect the abstraction.

Let’s consider a basic example of the Bridge Pattern using the abstraction of Shape and the implementation of Color. The goal is to have different shapes with different colors.

bridge design pattern

Example# 1:

Here’s a simple implementation in Java:

Example
/*
 * Author: Zameer Ali Mohil
 * */

// Abstraction: Shape interface
interface Shape {
    void draw();
}

// Implementor: Color interface
interface Color {
    void applyColor();
}

// Concrete Implementors: RedColor and GreenColor
class RedColor implements Color {
    @Override
    public void applyColor() {
        System.out.println("Applying Red Color");
    }
}

class GreenColor implements Color {
    @Override
    public void applyColor() {
        System.out.println("Applying Green Color");
    }
}

// Refined Abstractions: Triangle
class Triangle implements Shape {
    protected Color color;

    public Triangle(Color color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.print("Drawing Triangle ");
        color.applyColor();
    }
}

public class BridgePatternExample {
    public static void main(String[] args) {
        // Creating colors
        Color redColor = new RedColor();
        Color greenColor = new GreenColor();

        // Creating shapes with different colors
        Shape redTriangle = new Triangle(redColor);
        Shape greenTriangle = new Triangle(greenColor);

        // Drawing shapes with different colors
        redTriangle.draw();
        greenTriangle.draw();
    }
}

In this example:

  • The Shape interface represents the abstraction (e.g., a geometric shape).
  • The Color interface represents the implementation (e.g., the color of the shape).
  • RedColor and GreenColor are concrete implementors of the Color interface.
  • Triangle is a refined abstraction that extends the Shape interface and has a reference to the Color interface.
  • The BridgePatternExample demonstrates how to create instances of shapes (Triangle) with different colors (RedColor, GreenColor) without modifying the shape or color classes.

This way, you can easily introduce new shapes or colors without changing the existing classes. The Bridge pattern helps in decoupling abstraction and implementation, making the system more flexible and extensible.

Example# 2:

1. Define the Implementor Interface:

Example
// Implementor interface
interface Color {
    void applyColor();
}

2. Create Concrete Implementors:

Example
// Concrete Implementors
class RedColor implements Color {
    @Override
    public void applyColor() {
        System.out.println("Applying red color");
    }
}

class GreenColor implements Color {
    @Override
    public void applyColor() {
        System.out.println("Applying green color");
    }
}

3. Define the Abstraction Interface:

Example
// Abstraction interface
abstract class Shape {
    protected Color color;

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

    abstract void draw();
}

4. Create Refined Abstractions:

Example
// Refined Abstractions
class Circle extends Shape {
    public Circle(Color color) {
        super(color);
    }

    @Override
    void draw() {
        System.out.print("Drawing Circle: ");
        color.applyColor();
    }
}

class Square extends Shape {
    public Square(Color color) {
        super(color);
    }

    @Override
    void draw() {
        System.out.print("Drawing Square: ");
        color.applyColor();
    }
}

5. Client Code:

Example
// Client code
public class BridgePatternExample {
    public static void main(String[] args) {
        // Use the Bridge Pattern

        // Create different shapes with different colors
        Shape redCircle = new Circle(new RedColor());
        Shape greenSquare = new Square(new GreenColor());

        // Draw the shapes with their respective colors
        redCircle.draw();
        greenSquare.draw();
    }
}

In this example:

  • The Color interface is the implementor interface, defining the method applyColor.
  • The RedColor and GreenColor classes are concrete implementors, providing specific implementations of the color.
  • The Shape class is the abstraction interface, containing a reference to the Color interface.
  • The Circle and Square classes are refined abstractions, extending the Shape class and providing specific implementations of the draw method.
  • In the BridgePatternExample, we create different shapes (Circle and Square) with different colors, demonstrating how the Bridge Pattern allows us to vary the shape and color independently.

This example illustrates how the Bridge Pattern promotes flexibility by allowing the abstraction and implementation to evolve independently. It enables the client code to work with different combinations of shapes and colors without tightly coupling them.


advantages of BRIDGE DESIGN PATTERN

The Bridge design pattern provides several advantages that contribute to a more flexible and maintainable code structure. Here are some of the key advantages of using the Bridge design pattern:

1. Separation of Abstraction and Implementation: The primary advantage of the Bridge pattern is that it separates the abstraction (high-level components) from the implementation (low-level details). This separation allows both to evolve independently, making the system more flexible and easier to maintain.

2. Flexibility and Extensibility: The Bridge pattern enables you to add new abstractions and implementations independently. You can introduce new classes for both abstractions and implementations without modifying existing code. This flexibility makes it easier to extend the system with new features or variations.

3. Reduced Impact of Changes: Changes to either the abstraction or implementation have minimal impact on the other side. Modifying the abstraction does not affect the implementation, and vice versa. This reduces the ripple effect of changes and makes the system more robust.

4. Improved Code Reusability: By separating the abstraction and implementation, you can reuse existing abstractions with different implementations and vice versa. This reusability promotes a more modular and efficient codebase.

5. Enhanced Maintainability: The Bridge pattern enhances code maintainability by isolating changes. Modifications to the abstraction or implementation do not require changes to the other side. This isolation simplifies maintenance tasks and reduces the risk of introducing errors.

6. Support for Large Hierarchies: The Bridge pattern is particularly useful when dealing with large class hierarchies. Instead of creating a massive class hierarchy with all possible combinations of abstractions and implementations, the Bridge pattern allows you to manage them separately, avoiding the need for an exponentially growing class hierarchy.

7. Adaptability to Changes in Requirements: As the system evolves and requirements change, the Bridge pattern provides a framework that accommodates those changes. You can introduce new abstractions or implementations without affecting existing components, making it easier to adapt to evolving needs.

8. Improved Testing and Debugging: Since the abstraction and implementation are separate, testing and debugging can be focused on one aspect without being affected by the details of the other. This separation simplifies the testing process and makes it easier to identify and fix issues.

9. Promotion of Design Principles: The Bridge pattern aligns with design principles such as the Dependency Inversion Principle, which promotes dependency on abstractions rather than concrete implementations. This adherence to design principles contributes to a more maintainable and scalable architecture.

In summary, the Bridge design pattern provides a modular and extensible solution that allows for independent development and evolution of abstraction and implementation components. It addresses concerns related to flexibility, extensibility, and maintainability, making it a valuable pattern in software design.

Leave a Reply

Your email address will not be published. Required fields are marked *