-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBridgePattern.py
More file actions
100 lines (72 loc) · 1.63 KB
/
Copy pathBridgePattern.py
File metadata and controls
100 lines (72 loc) · 1.63 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Bridge Pattern - Allows two components to work with each other having own
interface.
Example - Gestures and Mouse are different input devices but they shares
the same set of methods. These methods are sent to two different output
devices (Screen and Audio) but with same set of methods. Bridge pattern
allows any input device to work with any output device
Reference: http://www.dofactory.com/javascript/bridge-design-pattern
"""
from abc import abstractmethod
class InputDevice:
def __init__(self, output):
self.output = output
class Gestures(InputDevice):
def tap(self):
self.output.click()
def swipe(self):
self.output.move()
def pan(self):
self.output.drag()
def pinch(self):
self.output.zoom()
class Mouse(InputDevice):
def click(self):
self.output.click()
def move(self):
self.output.move()
def down(self):
self.output.drag()
def wheel(self):
self.output.zoom()
class OutputDevice:
@abstractmethod
def click(self):
pass
@abstractmethod
def move(self):
pass
@abstractmethod
def drag(self):
pass
@abstractmethod
def zoom(self):
pass
class Screen(OutputDevice):
def click(self):
print 'Screen Select'
def move(self):
print 'Screen Move'
def drag(self):
print 'Screen Drag'
def zoom(self):
print 'Screen Zoom'
class Audio(OutputDevice):
def click(self):
print 'Sound: Ting'
def move(self):
print 'Sound: Swoosh'
def drag(self):
print 'Sound: Eeeee'
def zoom(self):
print 'Sound: Vrooom'
screen = Screen()
audio = Audio()
hand = Gestures(screen)
mouse = Mouse(audio)
hand.tap()
hand.swipe()
hand.pinch()
mouse.click()
mouse.move()
mouse.wheel()