Skip to content

Commit 6d8b1e3

Browse files
committed
add Bridge pattern
1 parent e63e75a commit 6d8b1e3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

bridge/Bridge.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Python Design Patterns: Bridge
3+
# Author: Jakub Vojvoda [github.com/JakubVojvoda]
4+
# 2016
5+
#
6+
# Source code is licensed under MIT License
7+
# (for more details see LICENSE)
8+
#
9+
10+
import sys
11+
12+
#
13+
# Implementor
14+
# defines the interface for implementation classes
15+
#
16+
class Implementor:
17+
def action(self):
18+
pass
19+
20+
#
21+
# Concrete Implementors
22+
# implement the Implementor interface and define concrete implementations
23+
#
24+
class ConcreteImplementorA(Implementor):
25+
def action(self):
26+
print("Concrete Implementor A")
27+
28+
class ConcreteImplementorB(Implementor):
29+
def action(self):
30+
print("Concrete Implementor B")
31+
32+
#
33+
# Bridge
34+
# decouple an abstraction from its implementation
35+
#
36+
class Bridge:
37+
def __init__(self, implementation):
38+
self._implementor = implementation
39+
40+
def operation(self):
41+
self._implementor.action()
42+
43+
44+
if __name__ == "__main__":
45+
bridge = Bridge(ConcreteImplementorA())
46+
bridge.operation()
47+
48+
bridge = Bridge(ConcreteImplementorB())
49+
bridge.operation()

0 commit comments

Comments
 (0)