-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.py
88 lines (64 loc) · 2.04 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
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
from abc import ABC, abstractmethod
# Abstract Interface (aka Handle) used by the client
class Website(ABC):
def __init__(self, implementation):
# encapsulate an instance of a concrete implementation class
self._implementation = implementation
def __str__(self):
return "Interface: {}; Implementation: {}".format(
self.__class__.__name__, self._implementation.__class__.__name__
)
@abstractmethod
def show_page(self):
pass
# Concrete Interface 1
class FreeWebsite(Website):
def show_page(self):
ads = self._implementation.get_ads()
text = self._implementation.get_excerpt()
call_to_action = self._implementation.get_call_to_action()
print(ads)
print(text)
print(call_to_action)
print("")
# Concrete Interface 2
class PaidWebsite(Website):
def show_page(self):
text = self._implementation.get_article()
print(text)
print("")
# Abstract Implementation (aka Body) decoupled from the client
class Implementation(ABC):
def get_excerpt(self):
return "excerpt from the article"
def get_article(self):
return "full article"
def get_ads(self):
return "some ads"
@abstractmethod
def get_call_to_action(self):
pass
# Concrete Implementation 1
class ImplementationA(Implementation):
def get_call_to_action(self):
return "Pay 10 $ a month to remove ads"
# Concrete Implementation 2
class ImplementationB(Implementation):
def get_call_to_action(self):
return "Remove ads with just 10 $ a month"
# Client
def main():
a_free = FreeWebsite(ImplementationA())
print(a_free)
a_free.show_page() # the client interacts only with the interface
b_free = FreeWebsite(ImplementationB())
print(b_free)
b_free.show_page()
a_paid = PaidWebsite(ImplementationA())
print(a_paid)
a_paid.show_page()
b_paid = PaidWebsite(ImplementationB())
print(b_paid)
b_paid.show_page()
if __name__ == "__main__":
main()