SoFunction
Updated on 2025-04-05

Detailed explanation of the bridge mode of Java design pattern (Bridge Pattern)

Bridge Pattern of Java Design Pattern

Overview

Bridge Pattern is a structural design pattern used to separate abstract parts from their implementation parts.

This pattern connects abstraction and implementation by combining relationships rather than inheritance relationships, making the code more extensible and maintainable.

The core idea of ​​the bridge mode is to decouple the abstraction and implementation, so that the two can be changed independently.

Pattern structure

The bridge mode includes the following parts:

  • Abstraction: Defines the abstract interface and holds a reference to the implementation part of the object.
  • Refined Abstraction: Expand the abstract part and add concrete functions.
  • Implementation part (Implementor): Define implementation interfaces and provide definitions of basic operations.
  • Concrete Implementor: Specific implementationImplementorInterface, providing specific operational implementation.

Mode Principle

The principle of the bridge pattern is to replace the inheritance relationship by combining relationships, separating the abstract parts and the implementation parts so that they can be changed independently. This can avoid the explosion of the number of classes and improve the scalability and maintenance of the code. Specifically:

  1. Abstraction and implementation separation: By introducing an implementation interface, the abstract part does not directly depend on the specific implementation, but depends on the interface.
  2. Independent Change: The abstract part and the implementation part can be changed and expanded independently without affecting each other.
  3. Runtime binding: At runtime, abstract parts and concrete implementation parts can be dynamically combined.

UML Class Diagram

Abstraction
    +operation()
    -Implementor: Implementor

RefinedAbstraction
    +operation()

Implementor
    +operationImpl()

ConcreteImplementorA
    +operationImpl()

ConcreteImplementorB
    +operationImpl()

Sample code

Let’s take graphic drawing as an example, assuming there are different types of graphics and different colors. With the bridge mode, we can separate the graphics and colors so that they can be changed independently.

  • Step 1: Define the implementation interfaceColor
// Implement the interface to define the color operationpublic interface Color {
    void applyColor(); // How to apply color}
  • Step 2: Implement specific colorsRedandGreen
// Specific red implementation class to implement the Color interfacepublic class Red implements Color {
    @Override
    public void applyColor() {
        ("Red color applied."); // Apply red    }
}

// Specific green implementation class to implement the Color interfacepublic class Green implements Color {
    @Override
    public void applyColor() {
        ("Green color applied."); // Apply green    }
}
  • Step 3: Defining abstract classesShape
// Abstract class Shape, holding a reference of type Colorpublic abstract class Shape {
    protected Color color; // References to the implementation part
    // Constructor, accepts a Color implementation object    protected Shape(Color color) {
         = color;
    }

    public abstract void draw(); // Abstract drawing method}
  • Step 4: Implement specific shapesCircle andRectangle
// Specific circular implementation class, extended from Shapepublic class Circle extends Shape {
    public Circle(Color color) {
        super(color); // Call the parent class constructor    }

    @Override
    public void draw() {
        ("Circle drawn. "); // Output graphic information        (); // Apply color    }
}

// Specific rectangle implementation class, extended from Shapepublic class Rectangle extends Shape {
    public Rectangle(Color color) {
        super(color); // Call the parent class constructor    }

    @Override
    public void draw() {
        ("Rectangle drawn. "); // Output graphic information        (); // Apply color    }
}
  • Step 5: Testing the bridge mode
// Test class, used to demonstrate the application of bridge modepublic class BridgePatternDemo {
    public static void main(String[] args) {
        // Create a red circle        Shape redCircle = new Circle(new Red());
        // Create a green rectangle        Shape greenRectangle = new Rectangle(new Green());

        // Draw a red circle        ();
        // Draw a green rectangle        ();
    }
}

Output

Circle drawn. Red color applied.
Rectangle drawn. Green color applied.

advantage

  • Separate abstraction and implementation: The bridge pattern separates abstraction and implementation, improving the scalability and flexibility of the code.
  • Follow the principle of opening and closing: The abstract and implementation parts can be expanded independently without affecting existing code.
  • Reduce the number of classes: Through combination rather than inheritance, explosive growth of classes can be avoided.

Application scenarios

The bridge mode is suitable for the following scenarios:

Multidimensional changes

  • When a class has multiple independently changing dimensions, such as the shape and color of a graph, these dimensions need to be expanded and changed independently, and these dimensions can be separated using the bridge mode so that they can be changed independently.
  • Example: Suppose we have different types of graphics (such as circles, rectangles) and different colors (such as red, green), and can separate the graphics and colors so that they can be changed independently. Each time you add a graph or color, you don’t need to modify the existing code, just add a new implementation.

Runtime binding

  • The bridge pattern can be used when it is necessary to dynamically change the combination relationship between the abstract part and the implementation part at runtime. Through the bridge mode, different implementations can be selected at runtime, enhancing system flexibility.
  • Example: Suppose we have a drawing application where users can choose different drawing tools (such as brushes, pencils) and different drawing styles (such as solid lines, dotted lines). They can use bridge mode to separate drawing tools and drawing styles, so that users can dynamically select different combinations at runtime.

Avoid too many inheritance levels

  • When the system needs to expand on multiple dimensions, if inheritance is used, it will cause explosive growth of classes. You can use the bridge pattern to separate these dimensions, reduce the number of classes, and avoid excessive inheritance levels.
  • Example: Suppose we have a graphics library that supports different graphics (such as circles, rectangles) and different drawing engines (such as OpenGL, DirectX). If you use inheritance, each adding a graphics or drawing engine requires adding multiple classes. Using bridge mode, you can separate the graphics and drawing engines, so that for each additional graphics or drawing engine, you only need to add a new implementation class to reduce the number of classes.

Summarize

Bridge Pattern is a structural design pattern designed to separate abstract parts from their implementation parts so that the two can be changed independently. The bridge pattern replaces inheritance relationships by combining relationships, decoupling abstraction and implementation, making the code more extensible and maintainable. Its core idea is to introduce an implementation interface so that the abstract part does not directly rely on concrete implementations, but depends on the interface, thereby achieving the effect of independent expansion and dynamic binding at runtime.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.