Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Bridge mode


May 27, 2021 Design mode


Table of contents


Bridge mode

Bridge is used to decouple abstraction with implementation, allowing the two to change independently. This type of design pattern belongs to the structural pattern, which de-couples the two by providing a bridge structure between abstraction and implementation.

This pattern involves an interface that serves as a bridge, making the functionality of the entity class independent of the interface implementation class. These two types of classes can be structured and changed without affecting each other.

Let's demonstrate the use of Bridge Pattern with the following example. Where you can use the same abstract class method but different bridge implementation classes to draw circles of different colors.

Introduced

Intent: Separate abstract parts from implementation parts so that they can all change independently.

Key solution: In a variety of situations that may change, using inheritance can cause class explosion problems that are not flexible to scale.

When to use: The implementation system may have multiple angle classifications, each of which may vary.

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

Key code: Abstract classes rely on implementation classes.

Application examples: 1, pig eight ring from the canopy Marshal rebirth to pig, the mechanism of rebirth birth will be divided into two levels of earth, namely: soul and body, the former is equivalent to abstraction, the latter is equivalent to realization. T hrough the delegation of function, the living spirit invokes the function of the physical object, so that the living spirit can be selected dynamically. 2, the switch on the wall, you can see the switch is abstract, do not care how to achieve concrete inside.

Pros: 1, abstraction, and implementation separation. 2 , excellent ability to expand. 3 , the realization of details to the customer transparent.

Cons: The introduction of bridging patterns increases the difficulty of understanding and designing the system, as the aggregation association is based on the abstraction layer, requiring developers to design and program against the abstraction.

Use scenarios: 1, if a system needs to add more flexibility between the abstract and concrete roles of the widget, avoid establishing static inheritance links between the two levels, and bridge patterns allow them to establish an association at the abstraction layer. 2 . Bridge mode is especially useful for systems that do not want to use inheritance or that result in a sharp increase in the number of system classes due to multi-level inheritance. 3 . A class has two dimensions that vary independently, and both dimensions need to be extended.

Note: For two dimensions that change independently, using briddle mode is no longer appropriate.

Realize

We have a DrawAPI interface as a bridge implementation and an entity class of RedCircle and GreenCircle that implements the DrawAPI interface. hape is an abstract class that uses drawAPI objects. BridgePatternDemo, our demo class uses the Shape class to draw circles of different colors.

Bridge mode

Step 1

Create a bridge implementation interface.

DrawAPI.java

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

Step 2

Create a solid 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 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 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

Verify the output.

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