首页 > 编程语言 >Python: Adapte Pattern

Python: Adapte Pattern

时间:2022-10-18 21:11:30浏览次数:46  
标签:code Target Python Pattern Adapter Adapte interface Adaptee

Adapte.py

## 适配器模式 Adapte Pattern  geovindu, Geovin Du eidt
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="")

 

调用: 

main.py

## 调用
if __name__ == '__main__':
        ##  适配器模式 Adapte Pattern
        print("Client: I can work just fine with the Target objects:")
        target =Adapter.Target()
        Adapter.client_code(target)
        print("\n")

        adaptee = Adapter.Adaptee()
        print("Client: The Adapte 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.Adapter()
        Adapter.client_code(adapter)

  

输出:

Client: I can work just fine with the Target objects:
Target: The default target's behavior.

Client: The Adapte 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.
Process finished with exit code 0

  

 

标签:code,Target,Python,Pattern,Adapter,Adapte,interface,Adaptee
From: https://www.cnblogs.com/geovindu/p/16804198.html

相关文章

  • Python class:定义类(入门必读)
    前面章节中已经提到,类仅仅充当图纸的作用,本身并不能直接拿来用,而只有根据图纸造出的实际物品(对象)才能直接使用。因此,Python 程序中类的使用顺序是这样的:创建(定义)类,也就是......
  • Python实验报告
                                                         ......
  • Python类对象的创建和使用
    通过前面章节的学习,我们已经学会如何定义一个类,但要想使用它,必须创建该类的对象。创建类对象的过程,又称为类的实例化。Python类的实例化对已定义好的类进行实例化,其......
  • python基础入门之模块
    python基础入门之模块索引取值与迭代取值的差异l1=[11,22,33,44,55]1.索引取值print(l1[2])#33 可以任意位置任意次数取值,不支持无序类型的数据取值。2.迭......
  • python模块/导入模块
    索引取值与迭代取值的差异l1=[1,2,3,4,5]1.索引取值可以任意位置任意次数的取值不支持无序类型的数据取值print(l1[3])print(l1[3])#可以直接获取任意位置的......
  • 进入python的世界_day17_python基础——了解模块、如何使用和导入模块、包的原理
    一、模块介绍1.什么是模块​ 其实我们前一阵已经接触过了,importxxx、fromxximportxxx​ 能够有一定功能的集合体就是模块,比如有某些功能的py文件,包含这个文件的......
  • python中展示json数据不换行(手动换行)
    https://blog.csdn.net/chichu261/article/details/82784904Settings—>keymap—>在搜索框输入wraps—>选择UseSoftWraps—>之后设置快捷键就可以了。针对第......
  • Python基础17
    今日内容概要索引取值与迭代取值的差异模块简介模块分类导入模块的两种句式导入模块补充说明循环导入问题判断文件类型模块的查找顺序绝刀导入与相对导入包概述......
  • Python爬虫(学习笔记)
    Python爬虫(学习笔记)  常见的反爬机制及应对策略名称描述解决方案/反反爬措施1.Headers 从用户的headers进行反爬是最常见的反爬策略,Headers是......
  • 【Python】第3章-7 求最大值及其下标
    本题要求编写程序,找出给定的n个数中的最大值及其对应的最小下标(下标从0开始)。输入格式:输入在第一行中给出一个正整数n(1<n≤10)。第二行输入n个整数,用空格分开。输出格式......