GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python 桥接模式 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 设计模式
设计模式 Python 设计模式 Python 工厂模式 Python 单例模式 Python 建造者模式 Python 原型模式 Python 适配器模式 Python 装饰器模式 Python 代理模式 Python 外观模式 Python 桥接模式 Python 组合模式 Python 享元模式 Python 观察者模式 Python 策略模式 Python 命令模式 Python 模板方法模式 Python 迭代器模式 Python 责任链模式 Python 状态模式 Python 访问者模式 Python 中介者模式 Python 备忘录模式 Python 解释器模式
Python 外观模式
Python 组合模式

Python 桥接模式

桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立变化。简单来说,就是把事物的多种变化维度拆分开,让每个维度都能独立扩展。

生活中的桥接模式比喻

想象一下遥控器和电视的关系:

  • 遥控器(抽象部分):提供控制接口(开关、音量、频道)
  • 电视(实现部分):具体执行操作(索尼电视、三星电视、小米电视)

遥控器不需要知道具体是哪种电视,电视也不需要知道是哪种遥控器,它们通过一个"桥梁"连接,各自可以独立升级换代。


为什么需要桥接模式?

传统方式的局限性

假设我们要设计一个图形绘制系统,支持不同形状和颜色:

实例

# 传统实现 - 类爆炸问题
class RedCircle:
    def draw(self):
        print("绘制红色圆形")

class BlueCircle:
    def draw(self):
        print("绘制蓝色圆形")

class RedRectangle:
    def draw(self):
        print("绘制红色矩形")

class BlueRectangle:
    def draw(self):
        print("绘制蓝色矩形")

# 每增加一种颜色或形状,都需要创建新的类
# 3种颜色 × 3种形状 = 9个类!

桥接模式的优势

实例

# 桥接模式实现
class Color:
    """颜色接口 - 实现部分"""
    def apply_color(self):
        pass

class Shape:
    """形状抽象 - 抽象部分"""
    def __init__(self, color):
        self.color = color  # 桥接的关键!
   
    def draw(self):
        pass

# 只需要:3种颜色 + 3种形状 = 6个类
# 而不是:3 × 3 = 9个类

桥接模式的核心结构

让我们通过一个完整的示例来理解桥接模式的组件:

四个关键角色

  1. 抽象部分(Abstraction):定义抽象接口,维护对实现部分的引用
  2. 具体抽象(RefinedAbstraction):扩展抽象接口
  3. 实现部分(Implementor):定义实现接口
  4. 具体实现(ConcreteImplementor):具体实现接口

完整代码示例:图形绘制系统

步骤 1:定义实现部分接口(颜色)

实例

from abc import ABC, abstractmethod

class Color(ABC):
    """颜色接口 - 实现部分"""
    @abstractmethod
    def apply_color(self):
        pass

class Red(Color):
    """具体实现 - 红色"""
    def apply_color(self):
        return "红色"

class Blue(Color):
    """具体实现 - 蓝色"""
    def apply_color(self):
        return "蓝色"

class Green(Color):
    """具体实现 - 绿色"""
    def apply_color(self):
        return "绿色"

步骤 2:定义抽象部分(形状)

实例

class Shape(ABC):
    """形状抽象 - 抽象部分"""
    def __init__(self, color: Color):
        self.color = color  # 桥接的关键:持有实现部分的引用
   
    @abstractmethod
    def draw(self):
        pass

class Circle(Shape):
    """具体抽象 - 圆形"""
    def draw(self):
        color_name = self.color.apply_color()
        print(f"绘制{color_name}的圆形")

class Rectangle(Shape):
    """具体抽象 - 矩形"""
    def draw(self):
        color_name = self.color.apply_color()
        print(f"绘制{color_name}的矩形")

class Triangle(Shape):
    """具体抽象 - 三角形"""
    def draw(self):
        color_name = self.color.apply_color()
        print(f"绘制{color_name}的三角形")

步骤 3:使用桥接模式

实例

# 客户端代码
def main():
    # 创建颜色实现
    red = Red()
    blue = Blue()
    green = Green()
   
    # 创建形状抽象,并桥接颜色实现
    red_circle = Circle(red)
    blue_rectangle = Rectangle(blue)
    green_triangle = Triangle(green)
   
    # 绘制图形
    print("=== 图形绘制结果 ===")
    red_circle.draw()        # 输出:绘制红色的圆形
    blue_rectangle.draw()    # 输出:绘制蓝色的矩形
    green_triangle.draw()    # 输出:绘制绿色的三角形
   
    # 灵活组合:红色矩形
    red_rectangle = Rectangle(red)
    red_rectangle.draw()     # 输出:绘制红色的矩形

if __name__ == "__main__":
    main()

实际应用场景

场景 1:消息发送系统

实例

from abc import ABC, abstractmethod

# 实现部分:消息发送方式
class MessageSender(ABC):
    @abstractmethod
    def send(self, message):
        pass

class EmailSender(MessageSender):
    def send(self, message):
        return f"通过邮件发送:{message}"

class SMSSender(MessageSender):
    def send(self, message):
        return f"通过短信发送:{message}"

class WeChatSender(MessageSender):
    def send(self, message):
        return f"通过微信发送:{message}"

# 抽象部分:消息类型
class Message(ABC):
    def __init__(self, sender: MessageSender):
        self.sender = sender
   
    @abstractmethod
    def send(self):
        pass

class UrgentMessage(Message):
    def send(self):
        message = "[紧急] " + self.get_content()
        return self.sender.send(message)
   
    def get_content(self):
        return "系统出现异常,请立即处理!"

class NormalMessage(Message):
    def __init__(self, sender: MessageSender, content):
        super().__init__(sender)
        self.content = content
   
    def send(self):
        return self.sender.send(self.content)

# 使用示例
def message_example():
    email_sender = EmailSender()
    sms_sender = SMSSender()
   
    # 紧急消息通过短信发送
    urgent_sms = UrgentMessage(sms_sender)
    print(urgent_sms.send())  # 通过短信发送:[紧急] 系统出现异常,请立即处理!
   
    # 普通消息通过邮件发送
    normal_email = NormalMessage(email_sender, "月度报告已生成")
    print(normal_email.send())  # 通过邮件发送:月度报告已生成

message_example()

场景 2:设备遥控系统

实例

# 实现部分:设备
class Device(ABC):
    @abstractmethod
    def turn_on(self):
        pass
   
    @abstractmethod
    def turn_off(self):
        pass
   
    @abstractmethod
    def set_volume(self, volume):
        pass

class TV(Device):
    def turn_on(self):
        return "电视已开启"
   
    def turn_off(self):
        return "电视已关闭"
   
    def set_volume(self, volume):
        return f"电视音量设置为:{volume}"

class Radio(Device):
    def turn_on(self):
        return "收音机已开启"
   
    def turn_off(self):
        return "收音机已关闭"
   
    def set_volume(self, volume):
        return f"收音机音量设置为:{volume}"

# 抽象部分:遥控器
class RemoteControl:
    def __init__(self, device: Device):
        self.device = device
   
    def toggle_power(self):
        return "切换电源状态"
   
    def volume_up(self):
        return "音量增加"
   
    def volume_down(self):
        return "音量减少"

class AdvancedRemoteControl(RemoteControl):
    def mute(self):
        return "静音模式"
   
    def set_channel(self, channel):
        return f"切换到频道:{channel}"

# 使用示例
def remote_example():
    tv = TV()
    radio = Radio()
   
    basic_remote = RemoteControl(tv)
    advanced_remote = AdvancedRemoteControl(radio)
   
    print(basic_remote.toggle_power())  # 切换电源状态
    print(advanced_remote.mute())       # 静音模式

remote_example()

桥接模式的优势对比

特性 传统方式 桥接模式
扩展性 差,需要修改现有代码 好,可以独立扩展
类数量 多,容易类爆炸 少,线性增长
维护性 困难,牵一发而动全身 容易,职责分离
灵活性 固定组合 运行时动态组合

最佳实践和注意事项

使用时机

✅ 适合使用桥接模式的情况:

  • 需要在多个维度上独立扩展时
  • 需要在运行时切换实现时
  • 避免永久性绑定 between abstraction and implementation 时
  • 当继承会导致类层次结构过于复杂时

❌ 不适合的情况:

  • 变化维度很少,不需要复杂设计时
  • 抽象和实现之间有强耦合关系时

常见错误及避免方法

实例

# 错误示例:没有真正解耦
class BadShape:
    def __init__(self, color_type):
        if color_type == "red":
            self.color = Red()
        elif color_type == "blue":
            self.color = Blue()
        # 仍然需要修改这个类来添加新颜色

# 正确做法:依赖注入
class GoodShape:
    def __init__(self, color: Color):  # 依赖接口,而不是具体实现
        self.color = color

练习任务

练习 1:扩展图形系统

请为图形系统添加以下功能:

  1. 新增一种颜色(黄色)
  2. 新增一种形状(椭圆形)
  3. 创建黄色椭圆形并绘制

练习 2:设计支付系统

设计一个支付系统,要求:

  • 支付方式:支付宝、微信支付、银行卡
  • 支付类型:普通支付、分期支付、组合支付
  • 使用桥接模式实现,确保支付方式和支付类型可以独立扩展

总结

桥接模式通过分离抽象和实现,提供了极大的灵活性。它的核心思想是:

  1. 识别变化维度:找出系统中可能独立变化的多个维度
  2. 建立桥梁:通过组合关系连接不同维度
  3. 独立扩展:每个维度都可以独立发展和变化
AI 思考中...
Python 外观模式
Python 组合模式

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.