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

Python: Bridge Pattern

时间:2022-10-19 22:33:10浏览次数:34  
标签:Bridge Abstraction Python Pattern self Implementation abstraction implementation

 

DuBridge.py
# 桥接模式  Bridge Pattern
# DuBridyge.py editor: geovindu, Geovin Du

from __future__ import annotations
from abc import ABC, abstractmethod


class 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"抽象:用的基本运算:\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"扩展抽象:使用的扩展操作:\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 implements
the Implementation interface using that platform's API.
"""


class ConcreteImplementationA(Implementation):
    def operation_implementation(self) -> str:
        return "具体实现Gevoin Du:这是在平台Geovin Du上的结果."


class ConcreteImplementationB(Implementation):
    def operation_implementation(self) -> str:
        return "具体实施涂聚文:这是在涂聚文平台上的结果."


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="")

  

main.py:

调用:

        ## 桥接模式  Bridge Pattern
        implementation = DuBridge.ConcreteImplementationA()
        abstraction = DuBridge.Abstraction(implementation)
        DuBridge.client_code(abstraction)

        print("\n")

        implementation = DuBridge.ConcreteImplementationB()
        abstraction = DuBridge.ExtendedAbstraction(implementation)
        DuBuilder.DuBridge(abstraction);

  

输出:

抽象:用的基本运算:
具体实现Gevoin Du:这是在平台Geovin Du上的结果.

  

标签:Bridge,Abstraction,Python,Pattern,self,Implementation,abstraction,implementation
From: https://www.cnblogs.com/geovindu/p/16808103.html

相关文章

  • python基础-字典常用操作
    1.通过key获取value  dict={key1:value1,key2:value2}  dict['key1']可获取到key1对应的value1  person={'name':'tt','age':13}print(person['age'])......
  • Python - jsonpath 简单使用
    第三方包使用的时候需要单独安装使用场景:快速提取接口返回的JSON串中的某一个字段的值importjsonimportjsonpathjson_str='''{"success":tru......
  • python__list&tuple
    1classmates=['Michael','Bob','Tracy']2print(classmates)3print(len(classmates))4print(classmates[-1])5classmates.append("adma")6print(clas......
  • UE5 中用 Python 接口创建 Level Sequence 与设置 TriggerEvent
    UE5中用Python接口创建LevelSequence与设置TriggerEvent本文内容可能只能在UE5下有用,未在UE4环境下实验过。背景遇到了一个美术需求,需要批量读取一段动画,制......
  • 力扣525(java&python)-连续数组(中等)
    题目:给定一个二进制数组nums,找到含有相同数量的0和1的最长连续子数组,并返回该子数组的长度。 示例1:输入:nums=[0,1]输出:2说明:[0,1]是具有相同数量......
  • Python学习路程——Day18
    Python学习路程——Day18包的具体使用''' 虽然在python3中对包的要求低了,不需要__init__.py文件也可以识别,但是为了兼容性考虑,我们最好还是要加上__init__.py 1、如果......
  • Python系列(1)- Python 简介、开发环境配置和基础语法
    Python是荷兰人GuidovanRossum(吉多·范罗苏姆,中国程序员称其为“龟叔”)在1990年初开发的一种解释型编程语言。Python源代码遵循GPL(GNUGeneralPublicLicense)......
  • python常用内置模块
    今日内容包的具体使用虽然python3对包的要求降低了不需要__init__.py也可以识别但是为了兼容性的考虑最好还是加上__init__.py1.如果只想用包中某几个模块那么还是......
  • python基础:软件开发目录规范
    0、编程思想的转变1.面条版阶段所有的代码都全部堆叠在一起#可以看作直接将所有的数据都放在c盘2.函数版阶段根据功能的不同封装不同的函数(通过小字......
  • 【转】如何利用Python爬虫爬取网页中图片(成功实现自动翻页至最后一页)
    【原文】https://blog.csdn.net/weixin_65423581/article/details/1225336461.模块的使用(1).random模块:主要是为了产生随机数作为写入jpg的名称(这里其实可以用字......