Bridge Pattern: Separate the abstract part from its implementation part so that they can all change independently. It is an object structure pattern, also known as Handle and Body pattern or Interface pattern.

Imagine that if we want to draw rectangle, circle, ellipse and square, we need at least four shape classes, but if the drawing graph needs to have different colors, such as red, green and blue, at this time, there are at least two design schemes as follows:

  1. A variety of color versions are available for each shape.
  2. Combine shapes and colors as needed

For the system with two change dimensions (that is, two change causes), the second scheme is adopted to design the system with fewer classes and more convenient system expansion. The second scheme is the application of bridge mode. The bridge pattern transforms inherited relationships into association relationships, which reduces class-to-class coupling and reduces code writing. For systems with two dimensions of change (that is, two causes of change), it is more convenient and concise to adopt the bridge mode. The bridge pattern transforms inherited relationships into association relationships, which reduces class-to-class coupling and reduces code writing.

The model structure

The bridge mode contains the following roles:

  • Abstraction: Abstract class, bridge class
  • RefinedAbstraction: Expanded abstract class
  • Implementor: An implementation class, an interface that is bridged
  • ConcreteImplementor: Concrete implementation class

Introduction to the source code

JDBC is based on Java to support a variety of database operations, but different database self-implementation and transfer protocol are not the same, Java for each kind of database to write an interface to support the implementation of database vendors, obviously against the principle of simplified design, here Java is to provide a set of interfaces for vendors to implement. A set of interfaces for developers to call, the combination of the two is the classic bridge pattern. Working with JDBC as a programmer looks like this:

    Class.forName("com.mysql.jdbc.Driver");
    String url = "";
    String user = "";
    String password = "";
    Connection con = DriverManager.getConnection(url, user, password);
    Statement statement = connection.createStatement();
    String sql = "insert into student (name,age) VALUE ('" + name + "'," + age + ")";
    statement.execute(sql);
Copy the code

Let’s take a look at some of the source code

private static Connection getConnection(String var0, Properties var1, Class<? > var2) throws SQLException { ClassLoader var3 = var2 ! = null ? var2.getClassLoader() : null; Class var4 = DriverManager.class; synchronized(DriverManager.class) { if (var3 == null) { var3 = Thread.currentThread().getContextClassLoader(); } } if (var0 == null) { throw new SQLException("The url cannot be null", "08001"); } else { println("DriverManager.getConnection(\"" + var0 + "\")"); SQLException var10 = null; Iterator var5 = registeredDrivers.iterator(); while(true) { while(var5.hasNext()) { DriverInfo var6 = (DriverInfo)var5.next(); if (isDriverAllowed(var6.driver, var3)) { try { println(" trying " + var6.driver.getClass().getName()); Connection var7 = var6.driver.connect(var0, var1); if (var7 ! = null) { println("getConnection returning " + var6.driver.getClass().getName()); return var7; } } catch (SQLException var8) { if (var10 == null) { var10 = var8; } } } else { println(" skipping: " + var6.getClass().getName()); } } if (var10 ! = null) { println("getConnection failed: " + var10); throw var10; } println("getConnection: no suitable driver found for " + var0); throw new SQLException("No suitable driver found for " + var0, "08001"); }}}Copy the code

Look at these lines of code

ClassLoader var3 = var2 ! = null ? var2.getClassLoader() : null; Class var4 = DriverManager.class; synchronized(DriverManager.class) { if (var3 == null) { var3 = Thread.currentThread().getContextClassLoader(); }}... . Connection var7 = var6.driver.connect(var0, var1);Copy the code

DriverManager gets the Connection from the driver of the database driver package through reflection and class loading, so it is the driver that is involved in the bridge mode. DriverManager is just a manager of a driver. We, as consumers, only care about Connection, not driver, because we operate through Connection. Driver acts as an abstract bridge class, Driver packages such as com.mysql.jdbc.driver implement the bridge class, and Connection is the object being bridged. The two dimensions here are:

  • Different database types (different drivers)
  • Different database connection information (URL, USERNAME,password)

Now imagine a scenario like this – we design a framework that needs to provide an API to the outside world, but one of the classes inside the framework needs to change frequently, which is very unstable, but the API we provide cannot change all the time. To insulate API methods from frequently changing code, consider adapter or bridge patterns.