七、结构型(桥接模式)

news/2024/10/20 2:22:34/

桥接模式

概念
桥接模式是一种结构型设计模式,旨在将抽象部分与其实现部分分离,使它们可以独立变化。它通过使用组合关系而非继承来实现接口和实现的解耦,从而提高系统的灵活性和可扩展性。


应用场景

  1. 多个维度的变化:当一个系统有多个变化维度,比如形状和颜色,使用桥接模式可以将这些维度分开处理,避免大量的子类。
  2. 需要避免类爆炸:在某些情况下,子类数量呈指数级增长,使用桥接模式可以有效减少类的数量。
  3. 抽象与实现解耦:当需要在不影响客户端的情况下,修改或扩展类的实现时,桥接模式非常有效。
  4. 需要统一接口:当需要为不同的实现提供一致的接口时,桥接模式可以帮助实现。

注意点

  • 系统复杂性:引入桥接模式会增加系统的复杂性,因为需要维护额外的桥接类。
  • 设计适用性:确保桥接模式适用于场景,避免在简单的场景中使用,导致不必要的复杂性。
  • 抽象与实现的角色:明确抽象与实现的角色分离,确保设计的清晰性。

核心要素

  1. 抽象类:定义抽象部分的接口,并持有对实现部分的引用。
  2. 具体抽象类:扩展抽象类,定义具体的抽象实现。
  3. 实现接口:定义实现部分的接口。
  4. 具体实现类:实现实现接口的具体实现类。

Java代码完整示例

java">// 实现接口
interface Implementor {void operationImpl();
}// 具体实现类A
class ConcreteImplementorA implements Implementor {@Overridepublic void operationImpl() {System.out.println("具体实现A的操作");}
}// 具体实现类B
class ConcreteImplementorB implements Implementor {@Overridepublic void operationImpl() {System.out.println("具体实现B的操作");}
}// 抽象类
abstract class Abstraction {protected Implementor implementor;public Abstraction(Implementor implementor) {this.implementor = implementor;}public abstract void operation();
}// 具体抽象类
class RefinedAbstraction extends Abstraction {public RefinedAbstraction(Implementor implementor) {super(implementor);}@Overridepublic void operation() {System.out.println("具体抽象类的操作");implementor.operationImpl(); // 委托给实现类}
}// 客户端
public class Client {public static void main(String[] args) {Implementor implementorA = new ConcreteImplementorA();Abstraction abstractionA = new RefinedAbstraction(implementorA);abstractionA.operation();Implementor implementorB = new ConcreteImplementorB();Abstraction abstractionB = new RefinedAbstraction(implementorB);abstractionB.operation();}
}

各种变形用法完整示例

  1. 使用多个具体实现
    添加更多的实现类,展示灵活性。

    代码示例

    java">// 额外的具体实现类C
    class ConcreteImplementorC implements Implementor {@Overridepublic void operationImpl() {System.out.println("具体实现C的操作");}
    }// 客户端
    public class ClientMultipleImplementors {public static void main(String[] args) {Implementor implementorA = new ConcreteImplementorA();Abstraction abstractionA = new RefinedAbstraction(implementorA);abstractionA.operation();Implementor implementorB = new ConcreteImplementorB();Abstraction abstractionB = new RefinedAbstraction(implementorB);abstractionB.operation();Implementor implementorC = new ConcreteImplementorC();Abstraction abstractionC = new RefinedAbstraction(implementorC);abstractionC.operation();}
    }
    
  2. 扩展抽象类
    添加新的抽象类以扩展功能。

    代码示例

    java">// 新抽象类
    abstract class ExtendedAbstraction extends Abstraction {public ExtendedAbstraction(Implementor implementor) {super(implementor);}public abstract void extendedOperation();
    }// 具体扩展抽象类
    class ConcreteExtendedAbstraction extends ExtendedAbstraction {public ConcreteExtendedAbstraction(Implementor implementor) {super(implementor);}@Overridepublic void operation() {System.out.println("具体扩展抽象类的操作");implementor.operationImpl();}@Overridepublic void extendedOperation() {System.out.println("具体扩展抽象类的扩展操作");}
    }// 客户端
    public class ClientExtendedAbstraction {public static void main(String[] args) {Implementor implementorB = new ConcreteImplementorB();ExtendedAbstraction extendedAbstraction = new ConcreteExtendedAbstraction(implementorB);extendedAbstraction.operation();extendedAbstraction.extendedOperation();}
    }
    
  3. 桥接模式与组合模式结合
    桥接模式与组合模式结合使用,可以处理更复杂的结构。

    代码示例

    java">// 组合模式的叶子类
    class Leaf implements Implementor {private String name;public Leaf(String name) {this.name = name;}@Overridepublic void operationImpl() {System.out.println("叶子: " + name);}
    }// 组合模式的组合类
    class Composite implements Implementor {private List<Implementor> children = new ArrayList<>();public void add(Implementor child) {children.add(child);}@Overridepublic void operationImpl() {for (Implementor child : children) {child.operationImpl();}}
    }// 客户端
    public class ClientComposite {public static void main(String[] args) {Composite composite = new Composite();composite.add(new Leaf("叶子1"));composite.add(new Leaf("叶子2"));Abstraction abstraction = new RefinedAbstraction(composite);abstraction.operation();}
    }
    

这些示例展示了桥接模式的基本用法及其变形。


http://www.ppmy.cn/news/1538737.html

相关文章

Linux下CMake入门

CMake的基础知识 什么是 CMake CMake 是一个跨平台的构建工具&#xff0c;主要用于管理构建过程。CMake 不直接构建项目&#xff0c;而是生成特定平台上的构建系统&#xff08;如 Unix 下的 Makefile&#xff0c;Windows 下的 Visual Studio 工程&#xff09;&#xff0c;然后…

FireFox简单设置设置

文章目录 一 设置不显示标签页1原来的样子2新的样子3操作方法 二 设置竖直标签页栏1 效果图2 设置方法 三 设置firefox不提醒更新 一 设置不显示标签页 1原来的样子 2新的样子 3操作方法 地址栏输入 about:config搜索icon,双击选项列表中browserchrome.site icons的值&#…

百度搜索引擎(SEO)优化师的未来将何去何从?

百度搜索引擎&#xff08;SEO&#xff09;优化师的未来将何去何从&#xff1f; 作为一名SEO专家&#xff08;林汉文&#xff09;&#xff0c;在过去的三年里&#xff0c;我深感自己与快速变化的百度SEO圈子逐渐脱节。然而&#xff0c;在最近重拾旧业&#xff0c;重新审视SEO特…

postman变量,断言,参数化

环境变量 1.创建环境变量 正式环境是错误的&#xff0c;方便验证环境变化 2.在请求中添加变量 3.运行前选择环境变量 全局变量 能够在任何接口访问的变量 console中打印日志 console.log(responseBody);//将数据解析为json格式 var data JSON.parse(responseBody); conso…

软件供应链十年:探索开源的增长、风险和未来

回顾软件供应链状况报告的 10 年既是一个里程碑&#xff0c;也是一次行动号召。在过去十年中&#xff0c;开源消费改变了软件开发的世界。我们看到了前所未有的创新&#xff0c;但也出现了新的挑战&#xff0c;特别是在管理软件供应链的安全性和完整性方面。 在 Sonatype&…

DHASH感知算法计算视频相邻帧的相似度

一个朋友想用python来读取视频帧&#xff0c;根据帧和帧之间相似度判断剪辑痕迹&#xff1b;但是最后发现并没什么用…… 原理就是遍历地读取图像相邻帧&#xff0c;将图像相邻帧前处理后&#xff0c;缩小什么的&#xff0c;计算d_hash,然后计算其汉明距离&#xff0c;然后把汉…

动手学深度学习60 机器翻译与数据集

1. 机器翻译与数据集 import os import torch from d2l import torch as d2l#save d2l.DATA_HUB[fra-eng] (d2l.DATA_URL fra-eng.zip,94646ad1522d915e7b0f9296181140edcf86a4f5)#save def read_data_nmt():"""载入“英语&#xff0d;法语”数据集"&qu…

38 Spring

38 Spring 参考资料 Spring-全面详解&#xff08;学习总结&#xff09; 基本概念 Spring理念 : 使现有技术更加实用 . 本身就是一个大杂烩 , 整合现有的框架技术。 Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器&#xff08;框架&#xff09;。 IOC本质 IOC全…