Bridging mode of design mode

Posted by shayman on Thu, 20 Jan 2022 15:12:20 +0100

1. Bridging mode

Bridge is used to decouple abstraction from realization, so that they can change independently. This type of design pattern belongs to structural pattern, which realizes the decoupling of abstraction and realization by providing a bridge structure between abstraction and realization.

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

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

2. Introduction

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

Main solution: when there are many possible changes, using inheritance will cause class explosion and inflexible expansion.

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

How to solve it: separate the multi angle classification, let them change independently and reduce the coupling between them.

Key code: abstract classes depend on implementation classes.

Application example:

1. Zhu Bajie was reincarnated from Marshal Tianpeng to pig. The mechanism of reincarnation divides the world into two levels: soul and body. The former is equivalent to abstraction and the latter is equivalent to realization. Through the delegation of functions, creatures call the functions of physical objects, so that creatures can choose dynamically.
2. The switch on the wall, you can see that the switch is abstract, regardless of how it is implemented.

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

Disadvantages: the introduction of bridging mode will increase the difficulty of system understanding and design. Since the aggregation association is based on the abstraction layer, developers are required to design and program for the abstraction.

Usage scenario:

1. If a system needs to add more flexibility between the abstract role and concrete role of components to avoid establishing static inheritance relationship between the two levels, they can establish an association relationship at the abstract level through bridging mode.
2. The bridging mode is especially suitable for those systems that do not want to use inheritance or 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 extended.

Note: for two independently changing dimensions, bridging mode is perfect.

3. Realization

We have a DrawAPI interface implemented as a bridge and entity classes RedCircle and GreenCircle that implement the DrawAPI interface. Shape is an abstract class that uses the objects of the DrawAPI. The BridgePatternDemo class uses the shape class to draw circles of different colors.

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 the DrawAPI interface to create an 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 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
Use Shape and DrawAPI classes to draw 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 and output the results:

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

Topics: Java Design Pattern