forked from faif/python-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.py
More file actions
Latest commit
40 lines (32 loc) · 1.08 KB
/
Copy pathbridge.py
File metadata and controls
40 lines (32 loc) · 1.08 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
# http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python
# ConcreteImplementor 1/2
classDrawingAPI1:
defdrawCircle(self, x, y, radius):
print('API1.circle at {}:{} radius {}'.format(x, y, radius))
# ConcreteImplementor 2/2
classDrawingAPI2:
defdrawCircle(self, x, y, radius):
print('API2.circle at {}:{} radius {}'.format(x, y, radius))
# Refined Abstraction
classCircleShape:
def__init__(self, x, y, radius, drawingAPI):
self.__x=x
self.__y=y
self.__radius=radius
self.__drawingAPI=drawingAPI
# low-level i.e. Implementation specific
defdraw(self):
self.__drawingAPI.drawCircle(self.__x, self.__y, self.__radius)
# high-level i.e. Abstraction specific
defresizeByPercentage(self, pct):
self.__radius*=pct
defmain():
shapes= (
CircleShape(1, 2, 3, DrawingAPI1()),
CircleShape(5, 7, 11, DrawingAPI2())
)
forshapeinshapes:
shape.resizeByPercentage(2.5)
shape.draw()
if__name__=="__main__":
main()