Detailed explanation of design mode bridge mode

Keywords: Mobile Java Database JDBC

Basic introduction

  • Bridge mode is a structural design mode.
  • Put the implementation and abstraction in two different class levels, so that the two levels can be changed independently.
  • Based on the minimum design principle of class, different classes are given different responsibilities through encapsulation, aggregation, inheritance and other behaviors.
  • Its main feature is to separate the abstraction from the behavior implementation, so as to maintain the independence of each part and to cope with their functional expansion.

UML class diagram

Bridge mode case

We take mobile phones as an example. Mobile phones have brands (Nokia, Motorola) and styles (folding, upright). We need to produce different brands and styles, such as folding Nokia, upright Motorola

"Abstract" - mobile phone brand, with the function of turning on and off

public interface PhoneBrand {
    void open();
    void close();
}

"Specific" - mobile phone brands Nokia and Moto

public class Nokia implements PhoneBrand {
    @Override
    public void open() {
        System.out.println("Nokia power on...");
    }

    @Override
    public void close() {
        System.out.println("Nokia shutdown...");
    }
}
public class Moto implements PhoneBrand {
    @Override
    public void open() {
        System.out.println("Motorola power on...");
    }

    @Override
    public void close() {
        System.out.println("Motorola shutdown...");
    }
}

"Abstract" - mobile phone class, which connects with brands in an aggregate way and acts as a "bridge" of * *

public abstract class AbsPhone{

    private PhoneBrand brand;

    public AbsPhone(PhoneBrand brand) {
        this.brand = brand;
    }

    protected void open(){
        brand.open();
    }

    protected void close(){
        brand.close();
    }
}

"Specific" - folding and upright cell phones

public class FoldingPhone extends AbsPhone{

    public FoldingPhone(PhoneBrand brand) {
        super(brand);
    }

    @Override
    protected void open() {
        System.out.print("Folding type - ");
        super.open();
    }

    @Override
    protected void close() {
        System.out.print("Folding type - ");
        super.close();
    }
}
public class UpRightPhone extends AbsPhone{

    public UpRightPhone(PhoneBrand brand) {
        super(brand);
    }

    @Override
    protected void open() {
        System.out.print("erect position - ");
        super.open();
    }

    @Override
    protected void close() {
        System.out.print("erect position - ");
        super.close();
    }
}

test

@Test
public void test(){
    AbsPhone p1 = new FoldingPhone(new Nokia());
    p1.open();
    p1.close();
    System.out.println();
    AbsPhone p2 = new UpRightPhone(new Moto());
    p2.open();
    p2.close();
}

Result

Fold - Nokia power on
 Fold - Nokia shutdown

Upright - Motorola power on
 Stand up - Motorola shutdown

If we want to create other types of mobile phones, we just need to change the way we create them.

Bridge mode analysis

  1. It realizes the separation of the abstract part and the implementation part, which greatly provides the flexibility of the system, and makes the abstract part and the implementation part independent, which helps the system to carry on the layered design, so as to produce a better structured system.
  2. For the high-level part of the system, you only need to know the interface between the abstract part and the implementation part, and the other parts are completed by the specific business.
  3. The bridge mode can reduce the number of subclasses and the cost of management and maintenance.
  4. The introduction of bridge mode increases the difficulty of system understanding and design. Because the aggregation relationship is established in the abstract layer, developers are required to design and program for the abstract.
  5. The bridge mode needs to identify two independent changing dimensions in the system correctly, so its scope of use has certain limitations, that is, it needs to have such application scenarios.

Application of bridge mode in JDBC

We usually use JDBC in Java Connect the database, but there are many kinds of databases (MySQL, Oracle...), their connection methods and protocols are different. Obviously, it is impossible to write an interface for each kind of database, which violates the principle of simplified design, so the Java designer provides a set of interfaces for the manufacturers to implement and a set of interfaces for users to call.

We need to write such code when using JDBC

Class.forName("Database driver name");
Connection conn = DriverManager.getConnection("data base url", "User name", "Password");

The process is as follows:

  • When Class.forName(), through the reflection mechanism, load the. Class file into the Java virtual machine memory, initialize the Driver class, execute the following code, and register a Driver with the DriverManager. DriverManager is a Driver container, which manages different drivers

    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
    
  • When we get the connection, DriverManager will return a corresponding database connection according to the driver

    @CallerSensitive
    public static Connection getConnection(String url,
        java.util.Properties info) throws SQLException {
        return (getConnection(url, info, Reflection.getCallerClass()));
    }
    

Practical application scenarios

For those systems that do not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance, the bridging mode is particularly suitable.

  • Bank transfer system
    • Transfer classification: online transfer, counter transfer, AMT transfer
    • Transfer user type: ordinary user, silver user, gold user
  • Message management
    • Message type: instant message, delay message
    • Message classification: SMS, email, QQ

Posted by drdapoo on Mon, 30 Mar 2020 04:00:30 -0700