Bridge Pattern

Bridge is used to decouple abstraction from implementation so that the two can vary independently. This type of design pattern is a structural pattern, which decouples the two by providing a bridge structure between abstraction and implementation.

This pattern involves an interface that acts as a bridge, so that the functionality of entity classes is independent of the interface implementation classes. These two types of classes can be structurally changed without affecting each other.

The purpose of the Bridge pattern is to separate abstraction from implementation so that they can vary independently. The pattern separates the abstract part of an object from its implementation part, allowing them to change independently. It connects the abstract and implementation parts through composition rather than inheritance.

We use the following example to demonstrate the usage of the Bridge Pattern. Here, we can use the same abstract class methods but different bridge implementation classes to draw circles of different colors.

Introduction

intent

Used to separate the abstract part from the implementation part so that they can vary independently.

Main problems solved

Avoids the class explosion problem caused by inheritance and provides a more flexible way to extend.

Use Case

The Bridge pattern is suitable when a system may be classified from multiple perspectives and each perspective may vary independently.

Implementation Approach

  • Separating multi-angle classification: Separates classification logic from different perspectives, allowing them to vary independently.
  • Reduce Coupling: Reduces coupling between abstraction and implementation.

Key code

  • Abstract class: Defines an abstract class as part of the system.
  • Implementation class: Defines one or more implementation classes that are associated with the abstract class through aggregation (rather than inheritance).

Application example

  • Reincarnation: The separation of soul (abstraction) and body (implementation), allowing the soul to choose different bodies.
  • Wall switch: The separation of switch (abstraction) and internal implementation (implementation); users do not need to care about the internal working mechanism of the switch.

Advantages

  • Separating abstraction from implementation: Improves the flexibility and maintainability of the system.
  • Strong extensibility: Abstraction and implementation can be extended independently.
  • Transparent implementation details: Users do not need to understand implementation details.

Disadvantages

  • Difficulty of understanding and design: The Bridge pattern increases the difficulty of understanding and designing the system.
  • Aggregation Association: Requires developers to design and program at the abstraction level.

Usage suggestions

  • Use the Bridge pattern when the system needs to increase flexibility between the abstraction role and the concrete role.
  • The Bridge pattern is especially suitable for systems where inheritance is undesirable or where multi-level inheritance causes a sharp increase in the number of classes.
  • Use the Bridge pattern when a class has two independently varying dimensions and both dimensions need to be extended.

Notes

  • The Bridge pattern is suitable for two independently varying dimensions, ensuring that they can be extended and changed independently.

Structure

The following are several key roles of the Bridge pattern:

  • Abstraction: Defines the abstract interface, usually containing a reference to the implementation interface.
  • Refined Abstraction: An extension of the abstraction, which can be a subclass of the abstract class or a concrete implementation class.
  • Implementor: Defines the implementation interface and provides the interface for basic operations.
  • Concrete Implementor: A concrete class that implements the implementation interface.

Implementation

We have a ... as the bridge implementation.DrawAPIInterfaces and implementationsDrawAPIEntity classes of the interfaceRedCircle、GreenCircle。Shapeis an abstract class that will useDrawAPIthe object.BridgePatternDemoClass usageShapeclass to draw circles of different colors.

桥接模式的 UML 图

Step 1

Create a bridge implementation interface.

DrawAPI.java

public interface DrawAPI { public void drawCircle(int radius, int x, int y); }

Step 2

Create an ImplementationDrawAPIConcrete bridge implementation classes for the interface.

RedCircle.java

public class RedCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: red, radius: " + radius +", x: " +x+", "+ y +"]"); } }

GreenCircle.java

public class GreenCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: green, radius: " + radius +", x: " +x+", "+ y +"]"); } }

Step 3

UsageDrawAPICreating abstract classes from interfacesShape。

Shape.java

public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI){ this.drawAPI = drawAPI; } public abstract void draw(); }

Step 4

Create an ImplementationShapeConcrete classes of the abstract class.

Circle.java

public class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } public void draw() { drawAPI.drawCircle(radius,x,y); } }

Step 5

UsageShapeandDrawAPIThe class draws circles of different colors.

BridgePatternDemo.java

public class BridgePatternDemo { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new RedCircle()); Shape greenCircle = new Circle(100,100, 10, new GreenCircle()); redCircle.draw(); greenCircle.draw(); } }

Step 6

Execute the program, output the results:

Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[  color: green, radius: 10, x: 100, 100]

Related article recommendations

Bridge Pattern

other extensions