-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbridge.py
More file actions
61 lines (43 loc) Β· 1.91 KB
/
Copy pathbridge.py
File metadata and controls
61 lines (43 loc) Β· 1.91 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Bridge pattern helps untangle (ΡΠ°ΡΠΏΡΡΡΠ²Π°ΡΡ) an unnecessary complicated class hierarchy.
When implementation of specific classes, are mixed with implementation of independent classes.
Problem:
- Unrelated parallel abstraction
- Implementation specific abstraction, implementation independent abstraction
Scenario:
- Implementation-independent circle abstraction, (properties of circle, and how scale it)
- Implementation-dependent circle abstraction, (how to draw a circle)
Solution:
- Separate the abstraction into two different class hierarchies
Abstract factory and adapter are related to bridge pattern.
"""
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()