This is the 13th day of my participation in the August More Text Challenge.More challenges in August

Bridges are used to decouple abstractions from implementations so that they can vary independently. This type of design pattern is structural in that it decouples abstraction and implementation by providing a bridge between them.

Separate the abstract parts from the implementation parts so that they can be changed independently. ———— Design Patterns GOF

In the bridge mode, the abstract part is separated from the implementation part, which is mainly composed of four categories of roles:

  • Implementor: An interface that provides basic operations for modules, abstracts the core interface of the class, and gives it to concrete classes to implement the core interface.
  • ConcreateImplementor: This class is mainly used to implement the different Implementor interfaces. It provides different implementations for the interfaces, and is used to replace the abstract interface and call the concrete implementation.
  • AbstractBridge: This is the core of the bridge pattern. It’s a rush class that acts as a bridge, contains an instance of the Implementor, and can define its own variety of methods. It can include both abstract and concrete business methods.
  • RedefinedBridge (Extension class) : It is a concrete implementation of an abstract bridge class that can be defined as required.

UML diagrams

Scene demonstration in the drawing board module, the need to draw graphics, graphics drawing is divided into drawing circle, drawing rectangle. The graphics drawn will have color, size distinction.

To inherit the relationship to implement a logical UML now

As can be seen from the UML diagram above. This design has too many inheritance levels, and the coupling degree is also very high. For example, now we need to increase the width, draw the ellipse and other requirements, but the change momentum is very large, and the extension is not good.

Dynamically configure UML using the bridge pattern

The code abstracts the core interface as well as the implementation

/** * abstracts the core method *@author Iflytek_dsw
 *
 */
interface DrawApi {
	public void drawApi(a);
}

/** * Draw a circle *@author Iflytek_dsw
 *
 */
class DrawCircle implements DrawApi{

	@Override
	public void drawApi(a) {
		System.out.println("Draw a circle"); }}/** * draw rectangle *@author Iflytek_dsw
 *
 */
class DrawRectangle implements DrawApi{

	@Override
	public void drawApi(a) {
		System.out.println("Draw a rectangle"); }}Copy the code

Bridge classes and corresponding implementations

/** * create a bridge * by combining the relationships@author Iflytek_dsw
 *
 */
abstract class DrawApiBridge {
	private DrawApi drawApi;

	public DrawApiBridge(DrawApi drawApi) {
		super(a);this.drawApi = drawApi;
	}
	
	public void color(a){ drawApi.drawApi(); }}class DrawRed extends DrawApiBridge{

	public DrawRed(DrawApi drawApi) {
		super(drawApi);
	}

	@Override
	public void color(a) {
		super.color();
		System.out.println("Draw red"); }}class DrawBlue extends DrawApiBridge{

	public DrawBlue(DrawApi drawApi) {
		super(drawApi);
	}

	@Override
	public void color(a) {
		super.color();
		System.out.println("Draw blue"); }}Copy the code

Client use

public class Client {

	/ * * *@param args
	 */
	public static void main(String[] args) {
		/** * draw a red circle */
		DrawApi drawApi = new DrawCircle();
		DrawRed drawRed = new DrawRed(drawApi);
		drawRed.color();
		
		/** * draw a green rectangle */
		DrawApi drawApi2 = new DrawRectangle();
		DrawBlue drawBlue = newDrawBlue(drawApi2); drawBlue.color(); }}Copy the code

This makes inheritance a lot easier to implement, lower the hierarchy. It is very convenient to extend the drawing ellipse and add new size properties.

conclusion

According to the above experience, in the bridge mode, the core part is extracted as an abstract interface, and the multi-dimensional information is abstracted into the bridge implementation category. Dimensionality reduction is carried out through the combination of bridge and interface to achieve the effect of reducing coupling.