This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
bridge.py
122 lines (79 loc) · 2.78 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Bridge pattern
Bridge is a structural design pattern. It separates responsibilities into two
orthogonal class hierarchies: implementation and interface. Bridge decouples an
abstraction from its implementation so that the two can vary independently.
The interface class encapsulates an instance of a concrete implementation class.
The client interacts with the interface class, and the interface class in turn
"delegates" all requests to the implementation class.
Adapter makes things work after they're designed; Bridge makes them work before
they are.
"""
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()
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()
# in a real world scenario, we could perform A/B testing of our website by
# choosing a random implementation
import random
impl = random.choice([ImplementationA(), ImplementationB()])
print(FreeWebsite(impl))
if __name__ == "__main__":
main()