Python 桥接模式讲解和代码示例

简介: Python 桥接模式讲解和代码示例

桥接是一种结构型设计模式 可将业务逻辑或一个大类拆分为不同的层次结构 从而能独立地进行开发

层次结构中的第一层 通常称为抽象部分 将包含对第二层 实现部分 对象的引用 抽象部分将能将一些 有时是绝大部分 对自己的调用委派给实现部分的对象 所有的实现部分都有一个通用接口 因此它们能在抽象部分内部相互替换

进一步了解桥接模式


使用示例桥接模式在处理跨平台应用 支持多种类型的数据库服务器或与多个特定种类 例如云平台和社交网络等 的 API 供应商协作时会特别有用

识别方法桥接可以通过一些控制实体及其所依赖的多个不同平台之间的明确区别来进行识别

概念示例

本例说明了桥接设计模式的结构并重点回答了下面的问题

  • 它由哪些类组成
  • 这些类扮演了哪些角色
  • 模式中的各个元素会以何种方式相互关联

main.py: 概念示例


from __future__ import annotationsfrom abc import ABC, abstractmethodclass Abstraction:    """    The Abstraction defines the interface for the "control" part of the two    class hierarchies. It maintains a reference to an object of the    Implementation hierarchy and delegates all of the real work to this object.    """
    def __init__(self, implementation: Implementation) -> None:        self.implementation = implementation
    def operation(self) -> str:        return (f"Abstraction: Base operation with:\n"
                f"{self.implementation.operation_implementation()}")class ExtendedAbstraction(Abstraction):    """    You can extend the Abstraction without changing the Implementation classes.    """
    def operation(self) -> str:        return (f"ExtendedAbstraction: Extended operation with:\n"
                f"{self.implementation.operation_implementation()}")class Implementation(ABC):    """    The Implementation defines the interface for all implementation classes. It    doesn't have to match the Abstraction's interface. In fact, the two    interfaces can be entirely different. Typically the Implementation interface    provides only primitive operations, while the Abstraction defines higher-    level operations based on those primitives.    """
    @abstractmethod
    def operation_implementation(self) -> str:        pass"""Each Concrete Implementation corresponds to a specific platform and implementsthe Implementation interface using that platform's API."""class ConcreteImplementationA(Implementation):    def operation_implementation(self) -> str:        return "ConcreteImplementationA: Here's the result on the platform A."class ConcreteImplementationB(Implementation):    def operation_implementation(self) -> str:        return "ConcreteImplementationB: Here's the result on the platform B."def client_code(abstraction: Abstraction) -> None:    """    Except for the initialization phase, where an Abstraction object gets linked    with a specific Implementation object, the client code should only depend on    the Abstraction class. This way the client code can support any abstraction-    implementation combination.    """
    # ...
    print(abstraction.operation(), end="")    # ...if __name__ == "__main__":    """    The client code should be able to work with any pre-configured abstraction-    implementation combination.    """
    implementation = ConcreteImplementationA()    abstraction = Abstraction(implementation)    client_code(abstraction)    print("\n")    implementation = ConcreteImplementationB()    abstraction = ExtendedAbstraction(implementation)    client_code(abstraction)


Output.txt: 执行结果



Abstraction: Base operation with:
ConcreteImplementationA: Here's the result on the platform A.
ExtendedAbstraction: Extended operation with:
ConcreteImplementationB: Here's the result on the platform B.
相关文章
|
2月前
|
开发框架 数据建模 中间件
Python中的装饰器:简化代码,增强功能
在Python的世界里,装饰器是那些静悄悄的幕后英雄。它们不张扬,却能默默地为函数或类增添强大的功能。本文将带你了解装饰器的魅力所在,从基础概念到实际应用,我们一步步揭开装饰器的神秘面纱。准备好了吗?让我们开始这段简洁而富有启发性的旅程吧!
56 6
|
23天前
|
存储 缓存 Java
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
58 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
26天前
|
数据挖掘 数据处理 开发者
Python3 自定义排序详解:方法与示例
Python的排序功能强大且灵活,主要通过`sorted()`函数和列表的`sort()`方法实现。两者均支持`key`参数自定义排序规则。本文详细介绍了基础排序、按字符串长度或元组元素排序、降序排序、多条件排序及使用`lambda`表达式和`functools.cmp_to_key`进行复杂排序。通过示例展示了如何对简单数据类型、字典、类对象及复杂数据结构(如列车信息)进行排序。掌握这些技巧可以显著提升数据处理能力,为编程提供更强大的支持。
32 10
|
2月前
|
Python
课程设计项目之基于Python实现围棋游戏代码
游戏进去默认为九路玩法,当然也可以选择十三路或是十九路玩法 使用pycharam打开项目,pip安装模块并引用,然后运行即可, 代码每行都有详细的注释,可以做课程设计或者毕业设计项目参考
78 33
|
2月前
|
JavaScript API C#
【Azure Developer】Python代码调用Graph API将外部用户添加到组,结果无效,也无错误信息
根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`members@odata.bind`被错误写为`members@odata_bind`,导致用户未成功添加。
52 10
|
2月前
|
数据可视化 Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
通过这些思维导图和分析说明表,您可以更直观地理解和选择适合的数据可视化图表类型,帮助更有效地展示和分析数据。
105 8
|
2月前
|
API Python
【Azure Developer】分享一段Python代码调用Graph API创建用户的示例
分享一段Python代码调用Graph API创建用户的示例
68 11
|
2月前
|
测试技术 Python
探索Python中的装饰器:简化代码,增强功能
在Python的世界中,装饰器是那些能够为我们的代码增添魔力的小精灵。它们不仅让代码看起来更加优雅,还能在不改变原有函数定义的情况下,增加额外的功能。本文将通过生动的例子和易于理解的语言,带你领略装饰器的奥秘,从基础概念到实际应用,一起开启Python装饰器的奇妙旅程。
57 11
|
2月前
|
Python
探索Python中的装饰器:简化代码,增强功能
在Python的世界里,装饰器就像是给函数穿上了一件神奇的外套,让它们拥有了超能力。本文将通过浅显易懂的语言和生动的比喻,带你了解装饰器的基本概念、使用方法以及它们如何让你的代码变得更加简洁高效。让我们一起揭开装饰器的神秘面纱,看看它是如何在不改变函数核心逻辑的情况下,为函数增添新功能的吧!
|
2月前
|
程序员 测试技术 数据安全/隐私保护
深入理解Python装饰器:提升代码重用与可读性
本文旨在为中高级Python开发者提供一份关于装饰器的深度解析。通过探讨装饰器的基本原理、类型以及在实际项目中的应用案例,帮助读者更好地理解并运用这一强大的语言特性。不同于常规摘要,本文将以一个实际的软件开发场景引入,逐步揭示装饰器如何优化代码结构,提高开发效率和代码质量。
74 6

热门文章

最新文章

推荐镜像

更多