• 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. */

  • 相关阅读:
    【傅里叶分析】复数基础知识
    qt-C++笔记之两个窗口ui的交互
    【C++】C/C++内存管理
    c++中类的继承与多态
    2024年春季3月退役的大学生士兵免试专升本单独报名的新政策
    Windows 图像处理组件(WIC)读写位深度24位的 bmp 文件
    RISC-V架构——物理内存属性和物理内存保护
    Servlet 综合案例:表白墙
    vim 中批量添加注释
    【uni-app从入门到实战】项目创建、基本配置、首页
  • 原文地址:https://blog.csdn.net/DU9999999/article/details/133780743