桥接模式 组合模式 java_结合JDK源码看设计模式——桥接模式

前言:

在我们还没学习框架之前,肯定都学过JDBC。百度百科对JDBC是这样介绍的【JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。】通过JDBC我们可以完成Java对关系型数据库的SQL操作。下面我们介绍的这种模式是JDBC中使用的设计模式。

一、定义

将抽象部分与它的具体实现部分分离,使他们都可以独立地变化。是通过组合方式建立两个类之间的联系,而不是继承

二、适用场景

1、抽象和具体实现之间增加更多灵活性

使用桥接模式可以避免在这两个之间建立静态的继承关系,而是去建立组合关系。

2、一个类存在两个(或多个)独立变化的维度,且这两个维度需要独立进行扩展

对于桥接模式可以这样理解,桥接就像一座桥,可以用来连接两个不同地方,这两个地方自由发展,中间的贸易是通过一座桥来连接。

3、不希望使用继承,或者是由于多层继承导致系统类的个数剧增

同第一点理解

三、JDBC中的桥接模式

首先我们弄清楚桥接模式中需要什么角色:

1、实现类接口

2、抽象类

3、实现接口类

4、继承抽象类

我们来看一下大概的图是怎么样的

224fd3f65e40bb9820da09d25d9e6ce3.png

从图中我们能很清楚的看到抽象类和接口之间是通过组合方式来关联,这样关联有什么好处呢?抽象类下面可以自行发展自己的子类,并且接口类也可以自己发展子类。两者之间互不影响。这正是我们上面所说的两个维度独立扩展。不要以为客户端只能调用使用抽象类,而是接口下面的实现类可以放入抽象类的子类中进行操作。大致了解完之后,JDBC中又是怎么实现桥接模式的呢?

我们对Driver接口一定不陌生。如果从桥接模式来看,Driver就是一个接口,下面可以有MySQL的Driver,Oracle的Driver,这些就可以当做实现接口类。那么我们现在来看看MySQL中的Driver类

public class Driver extends NonRegisteringDriver implementsjava.sql.Driver {public Driver() throwsSQLException {

}static{try{

DriverManager.registerDriver(newDriver());

}catch(SQLException var1) {throw new RuntimeException("Can't register driver!");

}

}

}

特别简短的代码,其实只调用了DriverManager中的registerDriver方法来注册驱动。当驱动注册完成后,我们就会开始调用DriverManager中的getConnection方法了

public classDriverManager {public staticConnection getConnection(String url,

String user, String password)throwsSQLException {

java.util.Properties info= newjava.util.Properties();if (user != null) {

info.put("user", user);

}if (password != null) {

info.put("password", password);

}return(getConnection(url, info, Reflection.getCallerClass()));

}private staticConnection getConnection(

String url, java.util.Properties info, Class> caller) throwsSQLException {/** When callerCl is null, we should check the application's

* (which is invoking this class indirectly)

* classloader, so that the JDBC driver class outside rt.jar

* can be loaded from here.*/ClassLoader callerCL= caller != null ? caller.getClassLoader() : null;synchronized(DriverManager.class) {//synchronize loading of the correct classloader.

if (callerCL == null) {

callerCL=Thread.currentThread().getContextClassLoader();

}

}if(url == null) {throw new SQLException("The url cannot be null", "08001");

}

println("DriverManager.getConnection(\"" + url + "\")");//Walk through the loaded registeredDrivers attempting to make a connection.//Remember the first exception that gets raised so we can reraise it.

SQLException reason = null;for(DriverInfo aDriver : registeredDrivers) {//If the caller does not have permission to load the driver then//skip it.

if(isDriverAllowed(aDriver.driver, callerCL)) {try{

println(" trying " +aDriver.driver.getClass().getName());

Connection con=aDriver.driver.connect(url, info);if (con != null) {//Success!

println("getConnection returning " +aDriver.driver.getClass().getName());return(con);

}

}catch(SQLException ex) {if (reason == null) {

reason=ex;

}

}

}else{

println(" skipping: " +aDriver.getClass().getName());

}

}//if we got here nobody could connect.

if (reason != null) {

println("getConnection failed: " +reason);throwreason;

}

println("getConnection: no suitable driver found for "+url);throw new SQLException("No suitable driver found for "+ url, "08001");

}

}

}

上面是简化的代码,可以看到需要返回的是Connection对象。在Java中通过Connection提供给各个数据库一样的操作接口,这里的Connection可以看作抽象类。可以说我们用来操作不同数据库的方法都是相同的,不过MySQL有自己的ConnectionImpl类,同样Oracle也有对应的实现类。这里Driver和Connection之间是通过DriverManager类进行桥接的,不是像我们上面说的那样用组合关系来进行桥接。

四、总结

桥接模式其实特别好理解,只需要看一眼上面的UML类图,也许你就知道桥接模式的使用方法。JDBC这里使用桥接模式可以让Driver和Connection下面的类根据不同数据库来实现不同的发展。就像我们适用场景中的第二点。当然正如我们标题所说的结合着JDK源码来看设计模式。也许看完这篇博客你有自己的理解JDBC这里为什么要用桥接模式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值