Q: What are design patterns

Slowly: Design pattern is a solution for common scenarios in system service design, which can solve common problems encountered in the development of functional logic. Design pattern is not limited to the final implementation scheme, but in this conceptual pattern, to solve the code logic problems in system design.

Q: What is bridge mode

Slowly: The main role of the bridge pattern is to combine multiple matching uses by separating the abstract from the implementation.

The core implementation contains A class B interface in class A, passing the implementation of class B through constructors, and this class B is the bridge of the design.

For example, Bluetooth connection, mobile phones, watches have Bluetooth, Bluetooth can control the operation of TV, refrigerator, air conditioning. Bluetooth is the bridge between your phone and your TV.

Q: Can you understand it in code?

Take your time: let’s use the above example to create a device abstraction class that can call Bluetooth:

public abstract class Equipment {
    protected Bluetooth blueTooth;

    void connectBluetooth(Bluetooth blueTooth) {   // Connect to Bluetooth
        this.blueTooth = blueTooth;
    }
    
    abstract void use(a);  / / use
}
Copy the code
public class Phone implements Equipment {
    void use (a) {
        super.blueTooth.use(); }}Copy the code
public class Watch implements Equipment {
    void use (a) {
        super.blueTooth.use(); }}Copy the code

Bluetooth supported device interface

public interface Bluetooth {
    void use(a);
}
Copy the code
public class Television implements Bluetooth {
    void use(a) {
        System.out.println("Turn on the TV"); }}Copy the code
public class AirConditioner implements Bluetooth {
    void use(a) {
        System.out.println("Turn on the air conditioner"); }}Copy the code

test

public class Demo {
    public static void main(String[] args) {
        // Turn on the air conditioner with your cell phone
        Equipment equipment1 = new Phone();
        equipment1.connectBluetooth(new AirConditioner());
        equipment1.use();
        
        // Use the watch to turn on the TV
        Equipment equipment2 = new Phone();
        equipment1.connectBluetooth(newTelevision()); equipment1.use(); }}Copy the code

Q: It feels very similar to the previous adaptor mode. Is there any difference between them?

Slowly: The adapter pattern encapsulates the object that needs to be adapted to meet the specification that can be invoked by redefining a class.

The bridge mode enables it to be combined with multiple objects by injecting its own properties.