Bridge is used to decouple abstraction from implementation so that the two can change independently. This type of design pattern belongs to 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, making the function of the entity class independent of the interface implementation class. These two types of classes can be structurally changed without affecting each other.
We demonstrate the use of the bridging mode (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. 意图: Separate the abstract part from the implementation part so that they can be changed independently. 主要解决: In a variety of situations that may change, using inheritance can cause class explosion problems and make it inflexible to extend. 何时使用: The implementation system may be classified from multiple angles, each of which may change. 如何解决: Separate this multi-angle classification, let them change independently and reduce the coupling between them. 关键代码: The abstract class depends on the implementation class. 应用实例: 1. Zhu Bajie reincarnated from Marshal Tianpeng to pig, the mechanism of reincarnation divides the earthly world into two levels, namely: soul and body, the former is equivalent to abstraction, the latter is equivalent to realization. Through the delegation of the function, the creature invokes the function of the body object, so that the creature can choose dynamically. 2, the switch on the wall, the switch that you can see is abstract, regardless of how it is realized. 优点: 1. The separation of abstraction and implementation. 2. Excellent expansion ability. 3. The implementation details are transparent to customers. 缺点: The introduction of bridging pattern will increase the difficulty of system understanding and design. Because the aggregation relationship is established in the abstract layer, developers are required to design and program for abstraction. 使用场景: 1. If a system needs to add more flexibility between the abstract role and the concrete role of the component, and avoid establishing a static inheritance relationship between the two levels, they can establish an association at the abstract layer through the bridging mode. 2. Bridging mode is especially suitable for systems that do not want to use inheritance or that the number of system classes increases sharply because of multi-level inheritance. 3. There are two independent changing dimensions in a class, and both dimensions need to be extended. 注意事项: For two independently changing dimensions, bridging mode is a perfect fit. We have one implemented as a bridge DrawAPI Interface and implementation of the DrawAPI The entity class of the interface RedCircle 、 GreenCircle . Shape Is an abstract class that will use the DrawAPI The object of the. BridgePatternDemo Class usage Shape Class to draw circles of different colors. Create a bridge implementation interface. Create and implement the DrawAPI The entity bridge implementation class for the interface. Use DrawAPI Interface to create abstract classes Shape . Create and implement the Shape The entity class of the abstract class. Use Shape And DrawAPI Class draws circles of different colors. Execute the program and output the result: 6.9.1. Introduction ¶
6.9.2. Realize ¶
6.9.3. Step 1 ¶
DrawAPI.java ¶
publicinterfaceDrawAPI{publicvoiddrawCircle(intradius,intx,inty);}
6.9.4. Step 2 ¶
RedCircle.java ¶
publicclassRedCircleimplementsDrawAPI{@OverridepublicvoiddrawCircle(intradius,intx,inty){System.out.println("Drawing
Circle[ color: red, radius:"+radius+", x:"+x+","+y+"]");}}
GreenCircle.java ¶
publicclassGreenCircleimplementsDrawAPI{@OverridepublicvoiddrawCircle(intradius,intx,inty){System.out.println("Drawing
Circle[ color: green, radius:"+radius+", x:"+x+","+y+"]");}}
6.9.5. Step 3 ¶
Shape.java ¶
publicabstractclassShape{protectedDrawAPIdrawAPI;protectedShape(DrawAPIdrawAPI){this.drawAPI=drawAPI;}publicabstractvoiddraw();}
6.9.6. Step 4 ¶
Circle.java ¶
publicclassCircleextendsShape{privateintx,y,radius;publicCircle(intx,inty,intradius,DrawAPIdrawAPI){super(drawAPI);this.x=x;this.y=y;this.radius=radius;}publicvoiddraw(){drawAPI.drawCircle(radius,x,y);}}
6.9.7. Step 5 ¶
BridgePatternDemo.java ¶
publicclassBridgePatternDemo{publicstaticvoidmain(String[]args){ShaperedCircle=newCircle(100,100,10,newRedCircle());ShapegreenCircle=newCircle(100,100,10,newGreenCircle());redCircle.draw();greenCircle.draw();}}
6.9.8. Step 6 ¶
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]
6.9.9. Recommendation of related articles ¶