-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge.py
41 lines (30 loc) · 1.2 KB
/
bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class DrawingAPIOne(object):
"""Implementation-specific abstraction: concrete class one"""
def draw_circle(self, x, y, radius):
print("API 1 drawing a circle at ({}, {} with radius {}!)".format(x, y, radius))
class DrawingAPITwo(object):
"""Implementation-specific abstraction: concrete class two"""
def draw_circle(self, x, y, radius):
print("API 2 drawing a circle at ({}, {} with radius {}!)".format(x, y, radius))
class Circle(object):
"""Implementation-independent abstraction: for example, there could be a rectangle class!"""
def __init__(self, x, y, radius, drawing_api):
"""Initialize the necessary attributes"""
self._x = x
self._y = y
self._radius = radius
self._drawing_api = drawing_api
def draw(self):
"""Implementation-specific abstraction taken care of by another class: DrawingAPI"""
self._drawing_api.draw_circle(self._x, self._y, self._radius)
def scale(self, percent):
"""Implementation-independent"""
self._radius *= percent
#Build the first Circle object using API One
circle1 = Circle(1, 2, 3, DrawingAPIOne())
#Draw a circle
circle1.draw()
#Build the second Circle object using API Two
circle2 = Circle(2, 3, 4, DrawingAPITwo())
#Draw a circle
circle2.draw()