在谈这个问题的时候,我们有必要先去了解一下什么是桥梁模式。 首先我们可以去理解一下桥梁模式的用意:即将抽象化(Abstraction)与实现(Implementation)脱耦,使得二者可以独立的变化。请注意以上用意中有三个关键词:抽象化、实现和脱耦。 (1)抽象化 存在于多个实体中的共同的概念性联系,就是抽象化。作为一个过程,抽象化就是忽略一些信息,从而把不同是实体当做同样的实体来对待。 (2)实现 即针对抽象化给出的具体实现。 (3)脱耦 耦合是指两个实体行为的某种强关联,如果将他们的强关联去掉,就被称为耦合的解脱,或者称为脱耦。脱耦是指将抽象化和实现化之间的耦合解脱开,或者将他们之间的强关联换成弱关联。
Through the above content, we have a preliminary understanding of the design pattern of the bridge model. So in Java applications, there is a very typical example of bridge mode, which is how the application is developed using the JDBC driver. String sql = "SQL statement for specific operation"; Load Drive Class.froname ("Driver's name");//Create connection Connection conn = drivermanager.getconnecttion ("URL", "User", "password"); Create statement or PreparedStatement preparedstatement PS = con.preparestatement (SQL); Execute SQL, if it is a query, then get ResultSet ResultSet rs = Ps.executequery (); The loop takes the value out of the resultset and encapsulates it into the data object while (Rs.next ()) {String uuid = rs.getstring ("uuid"); int age = Rs.getint (2); }//Close Rs.close (); Ps.close (); Con.close (); From the above example, we wrote the application that is developed for the JDBC-oriented API, which is equivalent to the interface of the abstract part of the bridge pattern. Then the JDBC driver implements the JDBC API, the driver is equivalent to the concrete implementation part of the bridge pattern, and the different databases, because the database implementation is not the same, the executable SQL is not exactly the same, so the implementation of the JDBC driver is not the same, That is, different databases will have different drive implementations. At this point, we already have an abstract part of the--JDBC API, with a specific implementation of the part-driver. So how do they get connected? It is clear that by DriverManager to bridge them, from a side view, DriverManager here is similar to a simple factory function, give the JDBC application needs to use the JDBC API, how to get it? Get the corresponding object by DriverManager. [] (http://i2.51cto.com/images/blog/201807/04/7a78e4328241beb987083f54fc0a3c40.png?x-oss-process=image/watermark,size_16,text_qduxq1rp5y2a5a6i,color_ffffff,t_100,g_ se,x_10,y_10,shadow_90,type_zmfuz3pozw5nagvpdgk=) from the diagram above, it can be seen that JDBC-based applications use the JDBC API, which is an abstract extension of database operations, and is counted as an abstraction of the bridge pattern. , and the specific interface implementation is driven to complete, driving this nature is equivalent to the realization of the bridge mode part, and bridging the way, no longer let the abstract part of the implementation of the part, but the use of similar factory practices, through the DriverManager to the abstraction and implementation of the part of the docking together, In order to realize partial decoupling of abstraction and realization.
Talking about bridge mode implementation JDBC