设计模式——桥接模式(Bridge Pattern)+ Spring相关源码

编程入门 行业动态 更新时间:2024-10-23 09:35:58

设计<a href=https://www.elefans.com/category/jswz/34/1771241.html style=模式——桥接模式(Bridge Pattern)+ Spring相关源码"/>

设计模式——桥接模式(Bridge Pattern)+ Spring相关源码

文章目录

  • 一、桥接模式定义
  • 二、定义
    • 2.1 举个例子
      • 2.1.1 定义Phone类 、 SoC类、Screen类
      • 2.1.2 定义SoC和Screen实现类
      • 2.1.3 使用Phone桥接SoC和Screen实现类
    • 2.2 JDK源码——AbstractSelectableChannel
    • 2.3 Spring源码——DataSourceTransactionManager
  • 三、其他设计模式

一、桥接模式定义

类型: 结构型模式
目的: 将抽象和实现分离,使它们都可以独立的变化。

其实这种模式我们经常使用,无非就是接口/抽象类JAVA组合的经典应用。

二、定义

2.1 举个例子

我们定义个手机类,假设我们每增加一个品牌型号更换配件就要扩展新类,就会导致类爆炸问题。
所以这里我们就要用JAVA组合代替继承去扩展新类。

2.1.1 定义Phone类 、 SoC类、Screen类

桥接类Phone

public class Phone{// 成员变量private String brand;   // 手机品牌private String model;   // 手机型号private SoC soc;private Screen screen;private long price; //价格public Phone(String brand, String model, SoC soc, Screen screen, long price) {this.brand = brand;this.model = model;this.soc = soc;this.screen = screen;this.price = price;}
}

处理器抽象类

public abstract class SoC{String model;   //型号public SoC(String model) {this.model = model;}
}

屏幕抽象类


public abstract class Screen{String model;   //型号public Screen(String model) {this.model = model;}
}

2.1.2 定义SoC和Screen实现类

处理器实现类

public class Qualcomm extends SoC {}

屏幕实现类

public class OLED extends Screen {}

2.1.3 使用Phone桥接SoC和Screen实现类

Qualcomm q680 = new Qualcomm("骁龙680")
OLED oled = new OLED("6.67 英寸OLED直屏")
new Phone("华为", "nove 11 se", q680, oled, 199900)Qualcomm q778g = new Qualcomm("骁龙778G")
OLED oled2 = new OLED("6.67 英寸OLED 柔性直屏")
new Phone("小米", "Redmi Note 12 Pro 极速版", q778g, oled2, 199900)

2.2 JDK源码——AbstractSelectableChannel

public abstract class AbstractSelectableChannel extends SelectableChannel {private final SelectorProvider provider;private SelectionKey[] keys = null;}

SelectorProvider 实现类

public abstract class SelectorProviderImpl extends SelectorProvider {}
public class WEPollSelectorProvider extends SelectorProviderImpl {}
public class WindowsSelectorProvider extends SelectorProviderImpl {}

SelectionKey 实现类

public abstract class AbstractSelectionKey extends SelectionKey {}
public final class SelectionKeyImpl extends AbstractSelectionKey {}

2.3 Spring源码——DataSourceTransactionManager

public class DataSourceTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, InitializingBean {@Nullableprivate DataSource dataSource;
}

DataSource实现类

public abstract class AbstractDataSource implements DataSource {}
public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {}
public class SimpleDriverDataSource extends AbstractDriverBasedDataSource{}
public class HikariDataSource extends HikariConfig implements DataSource, Closeable {}

三、其他设计模式

创建型模式
结构型模式

  • 1、设计模式——装饰器模式(Decorator Pattern)+ Spring相关源码

行为型模式

  • 1、设计模式——访问者模式(Visitor Pattern)+ Spring相关源码
  • 2、设计模式——中介者模式(Mediator Pattern)+ JDK相关源码
  • 3、设计模式——策略模式(Strategy Pattern)+ Spring相关源码
  • 4、设计模式——状态模式(State Pattern)
  • 5、设计模式——命令模式(Command Pattern)+ Spring相关源码
  • 6、设计模式——观察者模式(Observer Pattern)+ Spring相关源码
  • 7、设计模式——备忘录模式(Memento Pattern)
  • 8、设计模式——模板方法模式(Template Pattern)+ Spring相关源码
  • 9、设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
  • 10、设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
  • 11、设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码

更多推荐

设计模式——桥接模式(Bridge Pattern)+ Spring相关源码

本文发布于:2023-11-15 09:11:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   源码   桥接   Bridge   Pattern

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!