Bridge mode in design mode

Summary

Bridges are used to decouple abstraction from implementation so that they can change independently. This type of design pattern is structural and decouples them by providing a Bridge between abstraction and implementation.

This pattern involves an interface as a bridge that makes the functionality of entity classes independent of interface implementation classes. These two types of classes can be structurally altered without interacting with each other.

Intention: Separate Abstract parts from implementation parts so that they can change independently.

Main Solution: Inheritance can cause class explosion and is not flexible to expand in a variety of possible situations.

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

How to solve this problem: Separate this multi-angle classification so that they change independently and reduce the coupling between them.

Key Code: An abstract class depends on an implementation class.
Advantage:
1. Separate abstraction from implementation, strong expandability
2. Compliance with Open and Close Principles
3. Compliance with Composite Multiplexing Principle
4. Its implementation details are transparent to customers
Disadvantages:
1. Because the aggregation relationship is built on the abstraction layer, developers are required to design and program for abstraction to correctly identify the two independent changing dimensions in the system, which makes it more difficult to understand and design the system.

interpretative statement

When it comes to building roads before getting rich, we think of its importance. It connects relatively isolated economies, makes bilateral trade more convenient and economic cooperation more efficient. Bridging, which is called Bridge in English, means bridging.
Now suppose we want to make an abstract picture that uses colored blocks of various shapes to express the cultural diversity of the world, named Shapes and Colors.
What tools do you need to draw? A stack of brushes and a stack of rulers. Assume two tools are produced in Region A and B, Region A can only produce various shapes, Region B has many colors of pigments and is good at creating all kinds of color paints. Then we will promote economic cooperation between Region A and B. Let's first look at the shape products produced in Region A.

public interface Ruler {
    //Define the shape it produces
    public void regularize();
}

public class SquareRuler implements Ruler{

    @Override
    public void regularize() {
        System.out.println("□");
    }
}

public class TriangleRuler implements Ruler{
    @Override
    public void regularize() {
        System.out.println("△");
    }
}

public class CircleRuler implements Ruler{
    @Override
    public void regularize() {
        System.out.println("○");
    }
}

Region B has different colors and we simulate a brush class:
This refers to the ruler class as a member variable and is declared protected so that the brush class inherits and the constructor is injected into the ruler. This is the focus of bridge docking.

public abstract class Pen {
    protected Ruler ruler;//Reference to ruler shape

    public Pen(Ruler ruler) {
        this.ruler = ruler;
    }
    public abstract void draw();//Abstract method

}
//Black Pen
public class BlackPen extends Pen {
    public BlackPen(Ruler ruler) {
        super(ruler);
    }

    @Override
    public void draw() {
        System.out.print("black");
        ruler.regularize();
    }
}

//White Pen
public class WhitePen extends Pen {
    public WhitePen(Ruler ruler) {
        super(ruler);
    }

    @Override
    public void draw() {
        System.out.print("white");
        ruler.regularize();
    }
}

The Main function is as follows:

public class Main {
    public static void main(String[] args) {

        new WhitePen(new CircleRuler()).draw();
        new WhitePen(new SquareRuler()).draw();
        new WhitePen(new TriangleRuler()).draw();

        new BlackPen(new CircleRuler()).draw();
        new BlackPen(new SquareRuler()).draw();
        new BlackPen(new TriangleRuler()).draw();



    }

}

The results are as follows:

summary

One thing to note here is the difference between bridging mode and decorator mode:
Bridge mode is:
Color B1 wants to combine with shape A1, so B1 goes to Region B (abstract class) and gets shape A1 of Region A because Region B holds Region A (interface - >Ruler)So he owns all the shapes of Region A, and Region B gives shape A1 to color B1 to dye. It's important to note here that Region B only holds the interface of Region A. Region B can either use everything of Region A or choose not to use it, either B or A! But if B achieves A, then B is A, whether he wants to own it or not. This is different from Decorator mode.A very important point.
Decorator mode is:
Region B implements Region A, and you see that Region AB and all colors and shapes belong to the same type A. So as long as the parameters and return values that can be passed in by a method are of type A, you can nest indefinitely through that type, and any one and color can be combined with any shape, including yourself, so you can achieve nesting.

Tags: Java Design Pattern

Posted on Tue, 28 Sep 2021 12:37:06 -0400 by adslworld.net