zoukankan      html  css  js  c++  java
  • 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge

    Python版

    https://github.com/faif/python-patterns/blob/master/structural/bridge.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """
    *References:
    http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python
    
    *TL;DR80
    Decouples an abstraction from its implementation.
    """
    
    
    # ConcreteImplementor 1/2
    class DrawingAPI1(object):
    
        def draw_circle(self, x, y, radius):
            print('API1.circle at {}:{} radius {}'.format(x, y, radius))
    
    
    # ConcreteImplementor 2/2
    class DrawingAPI2(object):
    
        def draw_circle(self, x, y, radius):
            print('API2.circle at {}:{} radius {}'.format(x, y, radius))
    
    
    # Refined Abstraction
    class CircleShape(object):
    
        def __init__(self, x, y, radius, drawing_api):
            self._x = x
            self._y = y
            self._radius = radius
            self._drawing_api = drawing_api
    
        # low-level i.e. Implementation specific
        def draw(self):
            self._drawing_api.draw_circle(self._x, self._y, self._radius)
    
        # high-level i.e. Abstraction specific
        def scale(self, pct):
            self._radius *= pct
    
    
    def main():
        shapes = (
            CircleShape(1, 2, 3, DrawingAPI1()),
            CircleShape(5, 7, 11, DrawingAPI2())
        )
    
        for shape in shapes:
            shape.scale(2.5)
            shape.draw()
    
    
    if __name__ == '__main__':
        main()
    
    ### OUTPUT ###
    # API1.circle at 1:2 radius 7.5
    # API2.circle at 5:7 radius 27.5
    Python转载版
  • 相关阅读:
    黑域,黑阈 Permission denied
    手机闪存速度测试工具,AndroBench
    找进程的窗口Handle
    nginx http 正向代理
    windows命令行netstat 统计连接数
    FLIR ONE PRO热成像仪
    python2.0_s12_day14_jQuery详解
    python2.0_s12_day13_javascript&Dom&jQuery
    ssh&scp指定密钥
    apache+php生产环境错误记录
  • 原文地址:https://www.cnblogs.com/demonzk/p/9035376.html
Copyright © 2011-2022 走看看