Java design Pattern-bridging mode (bridge) _ Bridging mode

Source: Internet
Author: User

The purpose of bridging mode is to decouple the implementation of abstract and abstract methods so that they can be separated from each other.
To control multiple machines through a common interface, you can use adapter mode to create an adapter class for each controller that can be converted to a call to an existing controller by speaking standard interface calls.
General Abstraction: A method of bridging mode

Database Driven

If you connect with JDBC, just use a different class.forname parameter, and the rest will be the same. Oracle or MySQL, just a different kind of engagement, and the following are the same.
Class.forName (xxx.xx.xx) returns a class
The role of Class.forName (XXX.XX.XX) is to require the JVM to find and load the specified class, meaning that the JVM executes the static code snippet for that class (static code block in the jar package)
What's the difference between New and Class.forName ().
The difference is that the object is created in a different way, using the class-loading mechanism, which creates a new class. First, newinstance () is a method, and new is a keyword.
Second, the use of the Newinstance () under class is limited because it generates objects that can only invoke parameterless constructors, while using the new keyword to generate objects does not have this limitation.
Simply:
Newinstance (): weakly typed, inefficient, can only invoke parameterless constructs.
NEW: Strongly typed, relatively efficient, capable of invoking any public constructs.
Class.forName ("") returns a class.
Class.forName (""). Newinstance () returns an object.
The original class.forname is just equivalent to loading the class dynamically.
By querying the Java documentation we find that the purpose of using the Class.forName () static method is to dynamically load classes.
Usually, after the load is completed, the newinstance () static method under class is typically invoked to instantiate the object for manipulation. Therefore, it is no use simply to use Class.forName () to dynamically load classes, whose ultimate goal is to instantiate objects.
Why it is class.forname () that is useful when loading the database driver package, but does not invoke newinstance ().
Class.forName (""), the role is to require the JVM to find and load the specified class, first of all to understand that any class in Java to be loaded on the virtual machine to run, and static code is bound with class, class load success is that the execution of your static code, And I won't go through this static code again.
As we have said before, the role of Class.forName (XXX.XX.XX) is to require the JVM to find and load the specified class, and if there is a static initializer in the class, the JVM will inevitably execute the static code snippet for that class.
In the JDBC specification it is explicitly required that the driver class must register itself with DriverManager, that is, the code for any JDBC driver driver class must resemble the following:

public class Myjdbcdriver implements {static {Driver  
    ER (new Myjdbcdriver ()); }  
}  

Now that we have registered in the static initializer, we need only class.forname (xxx.xxx) when using JDBC; Even if you write. newinstance () The effect is the same.
When Class.forName ("Com.mysql.jdbc.Driver") is used, it is equivalent to calling the Java.sql.DriverManager.registerDriver in the Driver class (new Driver ()), instantiate a Driver, and then register in Java.sql.DriverManager.
I guess there is also a java.sql.DriverManager.registerDriver (new driver ()) in Oracle driver OJDBC6 class Oracle.jdbc.driver.OracleDriver. , register yourself to DriverManager.
The diagram above shows that a JDBC based application, using the JDBC API, is the equivalent of an abstract extension of the database operation, which counts as an abstraction of the bridging pattern, while the concrete interface implementation is driven by the driver, which naturally corresponds to the implementation part of the bridging pattern. And the way of bridging is no longer to let the abstract part hold the implementation part, but to adopt a similar factory practice, through the DriverManager to the abstraction part and the realization part, thus realizes the abstraction part and realizes the partial decoupling. The
JDBC architecture, which separates abstraction from concrete, allows the abstraction and specific parts to be expanded independently. For applications, a different driver can be used to allow programs to operate on different databases without having to change the application to migrate to different databases, and for drivers, implementing a different driver for the database does not affect the application. Moreover, this architecture of JDBC also reasonably divides the boundaries between the application developer and the driver developer.
in source code: Connection con = aDriver.driver.connect (URL, info); This is the point, tell us how Connection came. Adriver came from Registereddrivers, and Class.forName ("Com.mysql.jdbc.Driver"), when new a Driver registered to Registereddrivers, Again, return a connection instance with Driver.connect, which is a callback.

Class.forName is MySQL or Oracle, this Driver will certainly implement interface Java.sql.Driver, then through Drivermanager.registerdriver (new Driver ()); Make the DriverManager class hold a driver, whether you can take DriverManager as a bridge, as an abstract class in the bridge connection. Then hold an interface driver, as for MySQL or Oracle, don't care, wait for the reference. Because DriverManager holds a driver interface, what you pass on, I get the instantiation of what, and then I go through getconnection with your example, to call your own method connect, to get an example of connection.
Paste the code below, and then follow the process:

Package java.sql;  

Import Java.util.logging.Logger;  
    Public interface Driver {Connection connect (String URL, java.util.Properties info) throws SQLException;  
    Boolean acceptsurl (String url) throws SQLException; Driverpropertyinfo[] getpropertyinfo (String URL, java.util.Properties info) throws SQLException  
    ;  
    int getmajorversion ();  
    int getminorversion ();  
    Boolean jdbccompliant ();  
Public Logger Getparentlogger () throws sqlfeaturenotsupportedexception;  

}</span> Com.mysql.Driver class Import java.sql.SQLException;  
            public class Driver extends Nonregisteringdriver implements Java.sql.Driver {static {try {  
        Java.sql.DriverManager.registerDriver (New Driver ());  
        catch (SQLException E) {throw new RuntimeException ("Can ' t Register driver!"); Driver () throws SQLException {//Required for Class.forName (). NewinstanCE ()}}   

Since it is com.mysql.jdbc.Driver implementation, why not implement the driver connect method? The reason is Nonregisteringdriver, It implements the Java.sql.Driver interface, so driver does not need to implement it, driver the purpose is only to register, and to create a connection is done by Nonregisteringdriver. Last piece of Nonregisteringdriver code:

public class Nonregisteringdriver implements Java.sql.Driver {public  

     java.sql.Connection connect (String URL, Properties info)  
            throws SQLException {  
        ...  
        try {  
            Connection newconn = com.mysql.jdbc.ConnectionImpl.getInstance (  
                    host (props), port (props), props, Database (props), url);  
            return newconn;  
        } catch (SQLException Sqlex) {  
            ...  
            .. Throw Sqlex;  
        } catch (Exception ex) {  
            ...  
            .. Throw Sqlex  
        }}  
    }  
  

Java.sql.DriverManager class

public class DriverManager {public static synchronized void Registerdriver (java.sql.Driver Driver) throws SQLException {... registereddrivers.addifabsent (new Driverinfo (Driver  
         ));  
    .....  
    } private static Connection getconnection (String url, java.util.Properties info, class<?> caller) throws SQ Lexception {... for (Driverinfo adriver:registereddrivers) {//If the caller Doe  
            s not have permission to load the driver then//skip it.  
                    if (isdriverallowed (Adriver.driver, CALLERCL)) {try {//here is a callback  
         Connection con = aDriver.driver.connect (URL, info);  
.....  
    } }  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.