• Java实现桥接模式(设计模式 五)


    桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化,桥接模式通过提供一个桥接接口,将抽象类与具体实现类解耦,使它们可以独立地进行扩展。

    基本设计流程如下

    1. 定义抽象类或接口,作为实现的桥接点

    1. // 实现的桥接点接口
    2. interface Implementor {
    3. //原始功能
    4. void operationImpl();
    5. }

     2. 创建抽象部分的抽象类或接口

    创建抽象类或接口,抽象部分的接口应该包含对实现部分的引用,并定义抽象方法

    1. // 抽象类(抽象部分)
    2. abstract class Abstraction {
    3. //原始功能引用(桥接抽象部分和实现部分的关键)
    4. protected Implementor implementor;
    5. protected Abstraction(Implementor implementor) {
    6. this.implementor = implementor;
    7. }
    8. //扩展的新功能(抽象部分)
    9. abstract void operation();
    10. }

    3. 创建实现部分的具体实现类

    根据实现部分的接口,创建具体的实现类。每个具体实现类负责实现实现部分的接口。

    1. // 具体实现类A
    2. class ConcreteImplementorA implements Implementor {
    3. @Override
    4. public void operationImpl() {
    5. System.out.println("原始功能A");
    6. }
    7. }
    8. // 具体实现类B
    9. class ConcreteImplementorB implements Implementor {
    10. @Override
    11. public void operationImpl() {
    12. System.out.println("原始功能B");
    13. }
    14. }

    4. 创建抽象部分的具体扩展类

    每个具体扩展类继承自抽象部分的抽象类或实现抽象部分的接口,并实现抽象方法。在具体扩展类的方法中,调用实现部分的方法,以实现抽象部分和实现部分的交互。

    1. // 具体实现类A的扩展(抽象部分扩展类)
    2. class RefinedAbstractionA extends Abstraction {
    3. //构建含有原始功能的引用
    4. protected RefinedAbstractionA(Implementor implementor) {
    5. super(implementor);
    6. }
    7. //添加的新功能
    8. @Override
    9. void operation() {
    10. System.out.println("扩展原始A的操作");
    11. // super.implementor.operationImpl();
    12. //同时实现原始功能
    13. implementor.operationImpl();
    14. }
    15. }
    16. // 具体实现类B的扩展((抽象部分扩展类))
    17. class RefinedAbstractionB extends Abstraction {
    18. protected RefinedAbstractionB(Implementor implementor) {
    19. super(implementor);
    20. }
    21. @Override
    22. void operation() {
    23. System.out.println("扩展原始功能B的操作");
    24. implementor.operationImpl();
    25. }
    26. }

    5. 使用桥接模式进行测试

    创建具体实现类的实例,并将其传递给具体扩展类的构造函数,然后,调用具体扩展类的方法,触发抽象部分和实现部分的交互

    1. public class Main {
    2. public static void main(String[] args) {
    3. //原始实现类对象
    4. Implementor implementorA = new ConcreteImplementorA();
    5. Implementor implementorB = new ConcreteImplementorB();
    6. //同时实现两个功能A(抽象部分和实现部分)
    7. Abstraction abstractionA = new RefinedAbstractionA(implementorA);
    8. abstractionA.operation();
    9. System.out.println("------------------------------------");
    10. //同时实现两个功能B(抽象部分和实现部分)
    11. Abstraction abstractionB = new RefinedAbstractionB(implementorB);
    12. abstractionB.operation();
    13. }
    14. }
    15. /* 结果如下
    16. 扩展原始A的操作
    17. 原始功能A
    18. ------------------------------------
    19. 扩展原始功能B的操作
    20. 原始功能B
    21. */

  • 相关阅读:
    上传项目代码到github
    【云原生】docker环境中安装mysql、redis服务
    Skywalking全部
    每天一道算法题(四)——移动零(将数组中的零移到最后面)
    Flutter关于StatefulWidget中State刷新时机的一点实用理解
    【Android 逆向】ART 函数抽取加壳 ② ( 禁用 dex2oat 简介 | TurboDex 中禁用 dex2oat 参考示例 )
    uniapp地图自定义文字和图标
    招投标系统软件源码,招投标全流程在线化管理
    PyQt5快速开发与实战 9.2 数据库处理
    驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
  • 原文地址:https://blog.csdn.net/DU9999999/article/details/133780743