Created
February 17, 2014 16:04
-
-
Save atsuya046/9053360 to your computer and use it in GitHub Desktop.
GoF design pattern - Bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python""" | |
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment