将抽象与实现解耦,使两者各自独立变化,满足一个产品多维度变化的需求,个人理解相当于排列组合,将继承中的非同步变化的部分剥离成接口,解决类爆炸问题
实现部分
/**
* 角色(Implementor,由ConcreteImplementor实现)
*/
public interface Phone {
void call();
void connectInternet();
}
public class Phone2G implements Phone{
@Override
public void call() {
System.out.println("2G手机打电话......");
}
@Override
public void connectInternet() {
System.out.println("2G手机上网......");
}
}
public class Phone5G implements Phone{
@Override
public void call() {
System.out.println("5G手机打电话......");
}
@Override
public void connectInternet() {
System.out.println("5G手机上网......");
}
}
抽象部分
/**
* 角色(Abstraction,由RefinedAbstraction继承来扩展)
*/
public abstract class Brand {
protected Phone phone;
public Brand(Phone phone){
this.phone = phone;
}
public void call(){
phone.call();
}
public void connectInternet(){
phone.connectInternet();
}
}
public class Apple extends Brand{
public Apple(Phone phone) {
super(phone);
}
@Override
public void call() {
System.out.println("这是苹果手机");
super.call();
}
@Override
public void connectInternet() {
System.out.println("这是苹果手机");
super.connectInternet();
}
}
public class XiaoMi extends Brand{
public XiaoMi(Phone phone) {
super(phone);
}
@Override
public void call() {
System.out.println("这是小米手机");
super.call();
}
@Override
public void connectInternet() {
System.out.println("这是小米手机");
super.connectInternet();
}
}
测试类
public class Test {
public static void main(String[] args) {
Brand brand = new Apple(new Phone5G());
brand.call();
brand.connectInternet();
Brand brand2 = new XiaoMi(new Phone2G());
brand2.call();
brand2.connectInternet();
}
}
结果
jdbc源码应用
package com.mysql.cj.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
public Driver() throws SQLException {
}
static {
try {
// 此处通过静态代码块在初始化时就注册mysql的驱动
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
public static synchronized void registerDriver(java.sql.Driver driver)
throws SQLException {
registerDriver(driver, null);
}
public static synchronized void registerDriver(java.sql.Driver driver,
DriverAction da)
throws SQLException {
/* Register the driver if it has not already been added to our list */
if(driver != null) {
registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
} else {
// This is for compatibility with the original DriverManager
throw new NullPointerException();
}
println("registerDriver: " + driver);
}
public static Driver getDriver(String url)
throws SQLException {
println("DriverManager.getDriver(\"" + url + "\")");
Class<?> callerClass = Reflection.getCallerClass();
/**
*此处通过DriverManager自己维护的CopyOnWriteArrayList<DriverInfo>
*registeredDrivers获取驱动连接信息,个人认为是桥接模式中的抽象部分,
*实现部分是mysql的driver部分
**/
for (DriverInfo aDriver : registeredDrivers) {
if(isDriverAllowed(aDriver.driver, callerClass)) {
try {
if(aDriver.driver.acceptsURL(url)) {
// Success!
println("getDriver returning " + aDriver.driver.getClass().getName());
return (aDriver.driver);
}
} catch(SQLException sqe) {
// Drop through and try the next driver.
}
} else {
println(" skipping: " + aDriver.driver.getClass().getName());
}
}
println("getDriver: no suitable driver");
throw new SQLException("No suitable driver", "08001");
}