[learning notes] design mode - bridge mode

Keywords: Java Design Pattern

0 design mode

If you don't know about design patterns, you can learn what design patterns are through this article

https://blog.csdn.net/qq_42874315/article/details/120006447

1 bridge mode

Bridging is used to decouple abstraction from realization, so that they can change independently. Separate abstract and concrete implementations so that they can develop independently.

For example, the relationship between people and food is abstract, and food is also abstract. In this way, it can be considered that both people and food need to be expanded.

2 implementation ideas

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.

3 required classes

Here we introduce an example to help you better understand: people use watercolor pens to draw, different people can use different watercolor pens to draw, two-dimensional expansion.

  1. Bridge interface (watercolor pen)
  2. Bridge implementation class (blue watercolor pen, red watercolor pen...)
  3. Abstract class (human beings aggregate the bridge interface and construct this interface in the abstract class)
  4. The implementation class of the abstract class (you, me, him, use the aggregated interface to call in the method of implementing the abstract class)
  5. Test class

4 specific implementation

4.1 drawapi (bridge interface)

/**
 * Bridging interface
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:46
 */
public interface DrawAPI {
    void drawCircle(int radius, int x, int y);
}

4.2 bridge implementation class

4.2.1 GreenCircle

/**
 * Implements the entity bridging implementation class of the DrawAPI interface
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:48
 */
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 +"]");
    }
}

4.2.2 RedCircle

/**
 * Implements the entity bridging implementation class of the DrawAPI interface
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:47
 */
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 +"]");
    }
}

4.3 shape (abstract class)

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:49
 */
public abstract class Shape {
    protected DrawAPI drawAPI;

    public Shape(DrawAPI drawAPI) {
        this.drawAPI = drawAPI;
    }

    public abstract void draw();
}

4.4 circle (implementation class of shape)

/**
 * An entity class that implements the Shape abstract class
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:50
 */
public class Circle extends Shape{

    private int x, y, radius;

    public Circle(DrawAPI drawAPI, int x, int y, int radius) {
        super(drawAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    @Override
    public void draw() {
        drawAPI.drawCircle(radius,x,y);
    }
}

4.5 testing

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:52
 */
public class Test {
    public static void main(String[] args) {
        Shape redCircle = new Circle(new RedCircle(),100,100, 10);
        Shape greenCircle = new Circle(new GreenCircle(),100, 10,100);
        redCircle.draw();
        greenCircle.draw();
    }
}

5 expansion

5.1 defect assumption in the example

  1. If the Shape has another Square implementation class, you can add another BlackSquare bridge implementation class
  2. However, there is no restriction between the Shape implementation class and the bridged implementation class, so it may cause the situation of new Square(new RedCircle)
  3. In this case, it will still output "red circle" (there is a problem)
  4. Therefore, you should a subclass interface of DrawApi, such as CircleDrawAPI or SquareDrawApi, and let the specific bridge implementation class implement these subclasses
  5. At the same time, let the Shape implementation class no longer use the DrawApi interface for construction, but use the specific sub interface for construction, so that each type can be standardized
  6. For example, Square can only get BlackSquare, and Circle can only get RedCircle

5.2 example transformation

5.2.1 unchanged: drawapi (bridge interface)

/**
 * Bridging interface
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:46
 */
public interface DrawAPI {
    public void drawCircle(int radius, int x, int y);
}

5.2.2 add: sub interface of bridge interface (only for marking)

CircleDrawAPI

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 22:16
 */
public interface CircleDrawAPI extends DrawAPI {
}

SquareDrawAPI

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 22:16
 */
public interface SquareDrawAPI extends DrawAPI{
}

5.2.3 modification: implementation class of bridging sub interface (originally directly implementing bridging interface)

1. Implement CircleDrawAPI

GreenCircle

/**
 * Implements the entity bridging implementation class of the DrawAPI interface
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:48
 */
public class GreenCircle implements CircleDrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: green, radius: "
                + radius +", x: " +x+", "+ y +"]");
    }
}

RedCircle

/**
 * Implements the entity bridging implementation class of the DrawAPI interface
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:47
 */
public class RedCircle implements CircleDrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: red, radius: "
                + radius +", x: " +x+", "+ y +"]");
    }
}

2. Implement squaredraw API

BlackSquare

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 22:18
 */
public class BlackSquare implements SquareDrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Square[ color: black, radius: "
                + radius +", x: " +x+", "+ y +"]");
    }
}

5.2.4 unchanged: shape (abstract class)

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:49
 */
public abstract class Shape {
    protected DrawAPI drawAPI;

    public Shape(DrawAPI drawAPI) {
        this.drawAPI = drawAPI;
    }

    public abstract void draw();
}

5.2.5 modification: implementation class of abstract class (originally oriented to DrawAPI during construction, now oriented to the sub interface of DrawAPI)

Circle (modify the DrawAPI in the construction method to CircleDrawAPI)

/**
 * An entity class that implements the Shape abstract class
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:50
 */
public class Circle extends Shape{

    private int x, y, radius;

    public Circle(CircleDrawAPI drawAPI, int x, int y, int radius) {
        super(drawAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    @Override
    public void draw() {
        drawAPI.drawCircle(radius,x,y);
    }
}

Square (modify the DrawAPI in the construction method to SquareDrawAPI)

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 22:20
 */
public class Square extends Shape{
    private int x, y, radius;

    public Square(SquareDrawAPI drawAPI, int x, int y, int radius) {
        super(drawAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    @Override
    public void draw() {
        drawAPI.drawCircle(radius,x,y);
    }
}

5.2.6 testing

Using square to call GreenCircle will report an error and achieve the expected effect

/**
 * @Author ChenJiahao((Article 5)
 * @Date 2021/8/23 21:52
 */
public class Test {
    public static void main(String[] args) {
        Shape redCircle = new Circle(new RedCircle(),100,100, 10);
        // Error in new GreenCircle()
        // Shape greenCircle = new Square(new GreenCircle(),100, 10,100);
        Shape blackSquare = new Square(new BlackSquare(),100, 10,100);
        redCircle.draw();
        blackSquare.draw();
    }
}

6 mind map

7 example source code address

https://github.com/ChenJiahao0205/design-pattern/tree/master

last

I learned it through the video of Mr. Ma soldier and the rookie tutorial. Some of the contents may be the same

To read more articles related to design patterns, welcome to my columns [design patterns learning notes] and [design patterns]

After the release of 23 design pattern articles, I will publish a complete mind map, pay attention and don't get lost

Thank you for reading here. If there are any deficiencies in the article, you are welcome to point out; Yanzu point a praise, Yanzu point a praise, Yanzu point a praise, welcome to pay attention to the five programmers!

Posted by Khrysller on Sat, 25 Sep 2021 05:01:58 -0700