0 x0, introduction

I read The Beauty of Design Patterns in my spare time for daily updates. This paper corresponds to design patterns and paradigms: structural (49) and Bridge Pattern.

In the last section, we learned about the first structural design pattern, proxy pattern, adding functionality to the original class by introducing proxy classes without changing the original class (proxy-class).

The bridge mode can be understood in two ways:

  • Decouple abstractions and implementations so they can be developed independently; (Few application scenarios)
  • Abstract association is used to replace multi-level inheritance and the inheritance relation between classes is transformed into dynamic object combination relation. (Used more, to avoid multi-layer inheritance type explosion problem)

The bridging pattern is the practice of the DIP principle, from relying on one large and complete object to relying on two dimensions that can vary independently.

Say a little abstract, it doesn’t matter, later write an example to understand ~

Tips: Secondary knowledge processing inevitably has mistakes, interested in the time can refer to the original, thank you.


0x1, for example

For example, now we have a circle and a rectangle, which can abstract the shape parent class, and then they correspond to two subclasses:

abstract class Shape {
    abstract void show(a);
}

public class Circle extends Shape {
    @Override void show(a) { System.out.println("Circular"); }}public class Square extends Shape {
    @Override void show(a) { System.out.println("Rectangle"); }}Copy the code

If you add variations: colors, red and blue, combine with shapes to form the following four parent classes:

public class RedCircle extends Shape {
    @Override void show(a) { System.out.println("Red circle"); }}public class RedSquare extends Shape {
    @Override void show(a) { System.out.println("Red rectangle"); }}public class BlueCircle extends Shape {
    @Override void show(a) { System.out.println("Blue circle"); }}public class BlueSquare extends Shape {
    @Override void show(a) { System.out.println("Blue rectangle"); }}Copy the code

If the shape triangle and ellipse are added, then the number of subclasses becomes 4 * 2 = 8. If the color gold is added, then the number of subclasses becomes 4 * 3 = 12.

This problem can be solved by introducing the bridge pattern, where the two dimensions of change are clear: shape & color, which we understand as the abstract part and the latter as the implementation part, and bridge between them.

The implementation code is as follows:

// Color → Implementation section
public interface IColor {
    String draw(a);
}

// Red and blue → Implementation part concrete implementation
public class Red implements IColor {
    @Override public String draw(a) { return "Red"; }}public class Blue implements IColor {
    @Override
    public String draw(a) { return "Blue"; }}// Shape → Abstract part
abstract class Shape {
    // The shape holds the color reference, which is injected through the constructor, which is the bridging process
    protected IColor color;
    public Shape(IColor color) { this.color = color; }
    abstract void show(a);
}

// Circle, rectangle, abstract part extension
public class Circle extends Shape {
    public Circle(IColor color) { super(color); }
    @Override void show(a) { System.out.println(color.draw() + "Circular"); }}public class Square extends Shape {
    public Square(IColor color) { super(color); }
    @Override void show(a) { System.out.println(color.draw() + "Rectangle"); }}// Test case
public class CircleTest {
    public static void main(String[] args) {
        Shape redCircle = new Circle(new Red());
        Shape blueSquare = new Square(newBlue()); redCircle.show(); blueSquare.show(); }}Copy the code

The output is as follows:

Examples are very easy to understand, along with the introduction of the four roles, and the application of the bridge mode ~


0x2 Four Roles and Application Scenarios

  • Implementor: an interface for an implementation part, which can be interpreted as defining abstract behavior;
  • ConcreteImplementor: Concrete algorithms that implement abstract behavior;
  • Abstraction: Define an abstract category;
  • Refined Abstraction: Inheritance extends concrete characters;

The core principle of bridge mode:

The separation of abstraction from abstraction, the implementation of concrete classes depends on abstraction rather than on concrete, and the introduction completes the decoupling of concrete classes from concrete classes, using abstraction to combine or aggregate them rather than inheritance.

Application Scenarios:

  • A class has two or more dimensions that vary independently and all need to be extended independently;
  • 2. Do not want to use inheritance or cause a sharp increase in the number of classes in the system due to multi-level inheritance;
  • 3. More components need to be added under some unified protocol (such as payment scenario, it is expected to support wechat, Alipay and other payment components, and the unified protocol is collection, payment and deduction);
  • 4. Message-driven scenario (the message behavior is relatively uniform, including sending, receiving, processing and receipt, but the implementation of different apps is usually different);
  • 5, need to provide platform-independent applications (such as JDBC drivers for different databases, hard disk drivers, etc.);

Advantages: Separate entities and behaviors, better scalability, replace the multi-layer inheritance scheme, greatly reduce the number of subclasses; disadvantages

  • Increase the difficulty of design: it is necessary to focus on the abstraction layer at the beginning, and it requires a certain amount of experience to correctly identify the two independent dimensions;
  • Increased maintenance costs: Composition and aggregation are not as easy as inheritance to find simple call relationships for objects;
  • Performance degradation: The combination or aggregation relationship in OOP uses the way of delegating items, calling more objects, naturally affecting program performance;

Small eggs ~

How does the appliance/device at the beginning relate to the remote control? Left left left

The Remote contains a device reference, and all remotes can control the device through a common device Interface. The same device interface makes the remote control code can be used to control a variety of different devices, and inherit the controller parent class, and can achieve different remote controls.


References:

  • Bridge