JAVA设计模式之桥接模式
·
JAVA设计模式之桥接模式
文章目录
1. 定义
将具体部分与他的实现部分分离,使他们可以独立的变化
通过组合的方式建立两个类的联系,而不是继承
2. 类型
结构型
3. 适用场景
- 抽象和具体之间增加更多的灵活性,使用桥接模式避免了使这两个层次之间产生静态的继承关系,
通过桥接模式使他们建立关联关系,抽象部分和具体的实现部分都可以继承的方式独立扩展互不影响
- 一个存在两个(或多个)独立变化的维度,且这两个(或多个)维度都需要独立进行扩展
- 不希望使用继承,或因为多层继承导致系统类的个数剧增
4. 优点
- 分类抽象部分与具体实现部分
- 提高了系统的扩展性
- 符合开闭原则
- 符合合成复用原则
5. 缺点
增加了系统的理解与设计难度
需要正确识别出系统两个部分的唯独
6. 相关设计模式
-
桥接模式与组合模式
组合模式强调部分与整体的组合,桥接模式强调的是
-
桥接模式和适配器模式
适配器模式是改变已有的接口,让他们之间可以相互配合,桥接模式是分离抽象和具体的实现,目的是分离,
7. coding
7.1 银行账户接口
public interface IAccount {
IAccount openAccount();
void getAccountType();
}
7.2定期账号实现接口
public class DepositAccount implements IAccount {
@Override
public IAccount openAccount() {
System.out.println("这是一个定期账号");
return new DepositAccount();
}
@Override
public void getAccountType() {
System.out.println("这是一个定期账号类型");
}
}
7.3 活期账号实现接口
/**
* @ClassName SavingAccount
* @Description TODO
* @Author maido
* @Date 1:25 2022/10/28
* @Version 1.0
**/
public class SavingAccount implements IAccount {
@Override
public IAccount openAccount() {
System.out.println("打开活期账号");
return new SavingAccount();
}
@Override
public void getAccountType() {
System.out.println("这是活期账号");
}
}
7.4 银行抽象接口桥接账号方法
public abstract class Bank {
protected IAccount account;
public Bank(IAccount account) {
this.account = account;
}
abstract IAccount openAccount();
}
7.5 农业银行继承银行类
public class ABCBank extends Bank {
public ABCBank(IAccount account) {
super(account);
}
@Override
IAccount openAccount() {
System.out.println("打开中国农业银行");
return account;
}
}
7.6 工商银行继承银行类
public class ICBCBank extends Bank{
public ICBCBank(IAccount account) {
super(account);
}
@Override
IAccount openAccount() {
System.out.println("这是中国工商银行账户");
return account;
}
}
7.7 uml 关系图
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oPp4f26b-1666894174067)(C:\Users\maido\AppData\Roaming\Typora\typora-user-images\image-20221028014352476.png)]](https://i-blog.csdnimg.cn/blog_migrate/4d81af8b021be720c138d87971b2346f.png)
7.8 测试类
public class MainTest {
public static void main(String[] args) {
Bank icbcBank = new ICBCBank(new DepositAccount());
IAccount iAccount = icbcBank.openAccount();
iAccount.getAccountType();
Bank abcBank = new ABCBank(new SavingAccount());
IAccount openAccount = abcBank.openAccount();
openAccount.getAccountType();
}
}
8. 源码实现
8.1 Driver 、DriverManager 、 DriverInfo
8.1.1 来源
java.sql.Driver java.sql.DriverManager java.sql.DriverInfo
8.1.2 coding
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);
}
mysql
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
DriverManager是桥接的具体连接条件,通过注册驱动实现不同的驱动形式
更多推荐

所有评论(0)