-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbridgepattern.py
60 lines (46 loc) · 1.66 KB
/
bridgepattern.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from abc import ABC, abstractmethod
class LivingThings(ABC):
def __init__(self, breathe_implementor):
self.breathe_implementor = breathe_implementor
@abstractmethod
def breathe_process(self):
pass
class Dog(LivingThings):
def __init__(self, breathe_implementor):
super().__init__(breathe_implementor)
def breathe_process(self):
self.breathe_implementor.breathe()
class Fish(LivingThings):
def __init__(self, breathe_implementor):
super().__init__(breathe_implementor)
def breathe_process(self):
self.breathe_implementor.breathe()
class Tree(LivingThings):
def __init__(self, breathe_implementor):
super().__init__(breathe_implementor)
def breathe_process(self):
self.breathe_implementor.breathe()
class BreatheImplementor(ABC):
@abstractmethod
def breathe(self):
pass
class LandBreatheImplementation(BreatheImplementor):
def breathe(self):
print('breathes through nose')
print('inhale oxygen from air')
print('exhale carbon dioxide')
class WaterBreatheImplementation(BreatheImplementor):
def breathe(self):
print('breathes through gills')
print('inhale oxygen from water')
print('exhale carbon dioxide')
class TreeBreatheImplementation(BreatheImplementor):
def breathe(self):
print('breathes through leaves')
print('inhale carbon dioxide')
print('exhale oxygen through photosynthesis')
if __name__ == '__main__':
fish_obj = Fish(WaterBreatheImplementation())
fish_obj.breathe_process()
tree_obj = Tree(TreeBreatheImplementation())
tree_obj.breathe_process()