文章目录
第八章_桥接模式
1.介绍
1.1定义
1.2使用场景
1.2.1生活场景
1.2.2java场景
1.3角色
2.举例
2.1生活举例
abstract class TerrainType {
abstract void setTerrain();
}
class OnRoadTerrain extends TerrainType {
@Override
void setTerrain() {
System.out.println("在公路上行驶");
}
}
class OffRoadTerrain extends TerrainType {
@Override
void setTerrain() {
System.out.println("在草地上行驶");
}
}
class BeachTerrain extends TerrainType {
@Override
void setTerrain() {
System.out.println("在沙滩上行驶");
}
}
interface TireType {
void setTire();
}
class StandardTire implements TireType {
@Override
public void setTire() {
System.out.println("使用普通轮胎");
}
}
class OffRoadTire implements TireType {
@Override
public void setTire() {
System.out.println("使用越野轮胎");
}
}
class SnowTire implements TireType {
@Override
public void setTire() {
System.out.println("使用雪地轮胎");
}
}
class RemoteControlCar {
private TerrainType terrain;
private TireType tire;
public RemoteControlCar(TerrainType terrain, TireType tire) {
this.terrain = terrain;
this.tire = tire;
}
public void drive() {
this.terrain.setTerrain();
}
public void changeTire() {
this.tire.setTire();
}
}
public class BridgePatternExample {
public static void main(String[] args) {
TerrainType onRoad = new OnRoadTerrain();
TerrainType offRoad = new OffRoadTerrain();
TerrainType beach = new BeachTerrain();
TireType standard = new StandardTire();
TireType offRoadTire = new OffRoadTire();
TireType snowTire = new SnowTire();
RemoteControlCar car1 = new RemoteControlCar(onRoad, standard);
RemoteControlCar car2 = new RemoteControlCar(offRoad, offRoadTire);
RemoteControlCar car3 = new RemoteControlCar(beach, snowTire);
car1.drive();
car1.changeTire();
car2.drive();
car2.changeTire();
car3.drive();
car3.changeTire();
}
}
2.2JDK源码举例

2.2.1总览
public class JDBCTest {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://192.168.108.145/test", "root", "root");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from userinfo limit 1");
while(rs.next()){
System.out.println(rs.getString("id"));
}
rs.close();
}catch(SQLException se){
......
}
}
}
2.2.2Driver
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!");
}
}
public Driver() throws SQLException {
}
}
2.2.3DriverManager
public class DriverManager {
public static Connection getConnection(String url,
String user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();
if (user != null) {
info.put("user", user);
}
if (password != null) {
info.put("password", password);
}
return (getConnection(url, info, Reflection.getCallerClass()));
}
private static Connection getConnection(
String url, java.util.Properties info, Class<?> caller) throws SQLException {
ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
synchronized(DriverManager.class) {
if (callerCL == null) {
callerCL = Thread.currentThread().getContextClassLoader();
}
}
if(url == null) {
throw new SQLException("The url cannot be null", "08001");
}
println("DriverManager.getConnection(\"" + url + "\")");
SQLException reason = null;
for(DriverInfo aDriver : registeredDrivers) {
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
}
} else {
println(" skipping: " + aDriver.getClass().getName());
}
}
if (reason != null) {
println("getConnection failed: " + reason);
throw reason;
}
println("getConnection: no suitable driver found for "+ url);
throw new SQLException("No suitable driver found for "+ url, "08001");
}
}
3.优缺点
4.桥接模式与适配器模式
4.1桥接模式和适配器模式的区别和联系


4.2适配器与桥接模式的联合


