-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbridge.py
64 lines (51 loc) · 1.25 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
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Bridge.
Decouple an abstraction from its
implementation so that the two can vary
independently.
Computer <==> OS
| | | |
IBM HP Windows Linux
"""
# Implementor A
class Windows(object):
def run(self):
print("Running Windows!")
# Implementor B
class Linux(object):
def run(self):
print("Running Linux!")
# Abstraction
class Computer(object):
def __init__(self):
self.system = None
def install_system(self, system):
self.system = system
# RefinedAbstraction A
class IBM(Computer):
def run(self):
print("Open the IBM computer.");
if self.system != None:
self.system.run()
else:
print("The computer has not yet installed an operating system.")
# RefinedAbstraction B
class HP(Computer):
def run(self):
print("Open the HP computer.");
if self.system != None:
self.system.run()
else:
print("The computer has not yet installed an operating system.")
if __name__ == "__main__":
os_win = Windows()
os_linux = Linux()
device1 = IBM()
device2 = HP()
device1.install_system(os_win)
device2.install_system(os_linux)
device1.run()
device2.run()