Python 适配器模式讲解和代码示例

简介: Python 适配器模式讲解和代码示例

使用示例适配器模式在 Python 代码中很常见 基于一些遗留代码的系统常常会使用该模式 在这种情况下 适配器让遗留代码与现代的类得以相互合作

识别方法适配器可以通过以不同抽象或接口类型实例为参数的构造函数来识别 当适配器的任何方法被调用时 它会将参数转换为合适的格式 然后将调用定向到其封装对象中的一个或多个方法

概念示例

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

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

main.py: 概念示例

class Target:    """    The Target defines the domain-specific interface used by the client code.    """
    def request(self) -> str:        return "Target: The default target's behavior."class Adaptee:    """    The Adaptee contains some useful behavior, but its interface is incompatible    with the existing client code. The Adaptee needs some adaptation before the    client code can use it.    """
    def specific_request(self) -> str:        return ".eetpadA eht fo roivaheb laicepS"class Adapter(Target, Adaptee):    """    The Adapter makes the Adaptee's interface compatible with the Target's    interface via multiple inheritance.    """
    def request(self) -> str:        return f"Adapter: (TRANSLATED) {self.specific_request()[::-1]}"def client_code(target: "Target") -> None:    """    The client code supports all classes that follow the Target interface.    """
    print(target.request(), end="")if __name__ == "__main__":    print("Client: I can work just fine with the Target objects:")    target = Target()    client_code(target)    print("\n")    adaptee = Adaptee()    print("Client: The Adaptee class has a weird interface. "
          "See, I don't understand it:")    print(f"Adaptee: {adaptee.specific_request()}", end="\n\n")    print("Client: But I can work with it via the Adapter:")    adapter = Adapter()    client_code(adapter)

Output.txt: 执行结果

Client: I can work just fine with the Target objects:
Target: The default target's behavior.
Client: The Adaptee class has a weird interface. See, I don't understand it:
Adaptee: .eetpadA eht fo roivaheb laicepS
Client: But I can work with it via the Adapter:
Adapter: (TRANSLATED) Special behavior of the Adaptee.
相关文章
|
1天前
|
机器学习/深度学习 缓存 人工智能
令你膛目结舌的代码技巧 —— Python编程代码技巧
令你膛目结舌的代码技巧 —— Python编程代码技巧
8 2
|
1天前
|
存储 Python
10个小技巧,让你的 Python 代码更加优雅~
10个小技巧,让你的 Python 代码更加优雅~
10个小技巧,让你的 Python 代码更加优雅~
|
1天前
|
数据采集 XML 程序员
最新用Python做垃圾分类_python垃圾分类代码用key和format,5年经验Python程序员面试27天
最新用Python做垃圾分类_python垃圾分类代码用key和format,5年经验Python程序员面试27天
最新用Python做垃圾分类_python垃圾分类代码用key和format,5年经验Python程序员面试27天
|
1天前
|
数据采集 机器学习/深度学习 人工智能
最新用python代码画爱心,来自程序猿的浪漫~_python画爱心代码(1),2024年最新面试简历模板免费
最新用python代码画爱心,来自程序猿的浪漫~_python画爱心代码(1),2024年最新面试简历模板免费
最新用python代码画爱心,来自程序猿的浪漫~_python画爱心代码(1),2024年最新面试简历模板免费
|
1天前
|
测试技术 开发工具 iOS开发
Python如何快速定位最慢的代码?_pycharm找到执行时间长的代码(2)
Python如何快速定位最慢的代码?_pycharm找到执行时间长的代码(2)
Python如何快速定位最慢的代码?_pycharm找到执行时间长的代码(2)
|
1天前
|
数据采集 数据挖掘 测试技术
Python如何快速定位最慢的代码?_pycharm找到执行时间长的代码(1)
Python如何快速定位最慢的代码?_pycharm找到执行时间长的代码(1)
Python如何快速定位最慢的代码?_pycharm找到执行时间长的代码(1)
|
1天前
|
数据采集 数据挖掘 Python
最全妙不可言。写出优雅的 Python 代码的七条重要技巧,2024年最新被面试官怼了还有戏吗
最全妙不可言。写出优雅的 Python 代码的七条重要技巧,2024年最新被面试官怼了还有戏吗
|
1天前
|
设计模式 缓存 测试技术
Python装饰器,增强代码的魔力
在Python中,装饰器是一种设计模式,用于在不修改原始函数代码的情况下,给函数添加新的功能。装饰器本质上是一个函数,它接收一个函数作为参数并返回一个新的函数。这种用法在Python中非常强大,因为它允许开发者以一种非侵入性的方式增强现有代码。
|
1天前
|
Python
2024年Python最新刷爆全网的动态条形图,原来5行Python代码就能实现!,2024年最新Python面试必问的HashMap
2024年Python最新刷爆全网的动态条形图,原来5行Python代码就能实现!,2024年最新Python面试必问的HashMap
2024年Python最新刷爆全网的动态条形图,原来5行Python代码就能实现!,2024年最新Python面试必问的HashMap
|
1天前
|
存储 缓存 API
python源码解读_python代码解释
python源码解读_python代码解释