bridge mode


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

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

We demonstrate the usage of Bridge Pattern through the following example. Among them, you can use the same abstract class method but different bridge implementation classes to draw circles of different colors.

Introduction

Intent: Separate the abstract part from the implementation part so that they can change independently.

Main solution: In cases where there are many possible changes, using inheritance will cause a class explosion problem and make expansion inflexible.

When to use: The implementation system may have multiple angle classifications, and each angle may change.

How to solve: Separate this multi-angle classification, let them change independently, and reduce the coupling between them.

Key code: Abstract class depends on implementation class.

Application examples: 1. Zhu Bajie was reincarnated from Marshal Tianpeng to a pig. The mechanism of reincarnation divides the world into two levels, namely: soul and body. The former is equivalent to abstraction. , the latter is equivalent to realization. Through function delegation, living beings call the functions of physical objects, allowing living beings to make dynamic choices. 2. For the switch on the wall, the switch you can see is abstract, and you don’t need to worry about how it is implemented in detail.

Advantages: 1. Separation of abstraction and implementation. 2. Excellent expansion capabilities. 3. Make implementation details transparent to customers.

Disadvantages: The introduction of the bridge mode will increase the difficulty of understanding and designing the system. Since the aggregation relationship is established at the abstraction layer, developers are required to design and program based on abstraction.

Usage scenarios: 1. If a system needs to add more flexibility between the abstract role and the concrete role of the component, avoid establishing a static relationship between the two levels. Inheritance relationship, through the bridge mode, they can establish an association relationship at the abstract layer. 2. The bridge mode is particularly suitable for systems that do not want to use inheritance or where the number of system classes increases sharply due to multi-level inheritance. 3. A class has two independently changing dimensions, and both dimensions need to be expanded.

Notes: For two independently changing dimensions, the bridge mode is most suitable.

Implementation

We have a DrawAPI interface implemented as a bridge and an entity classRedCircle that implements the DrawAPI interface, GreenCircle. Shape is an abstract class that will use objects from DrawAPI. BridgePatternDemo, our demo class uses the Shape class to draw circles of different colors.

bridge_pattern_uml_diagram.jpg

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 entity bridge implementation class that implements the DrawAPI 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

Use DrawAPI interface creates abstract class Shape.

Shape.java

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

Step 4

Create an entity class that implements the Shape interface.

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

Use the Shape and DrawAPI classes to draw different Color circles.

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

Verify the output.

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