-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacade.py
88 lines (70 loc) · 1.85 KB
/
facade.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
# Subsystem1
class Amplifier:
def on(self):
print("Amplifier is on")
def off(self):
print("Amplifier is off")
def set_volume(self, volume):
print(f"Amplifier volume set to {volume}")
# Subsystem2
class DVDPlayer:
def on(self):
print("DVD Player is on")
def off(self):
print("DVD Player is off")
def play(self, movie):
print(f"DVD Player is playing {movie}")
# Subsystem3
class Projector:
def on(self):
print("Projector is on")
def off(self):
print("Projector is off")
def wide_screen_mode(self):
print("Projector in widescreen mode")
# Facade
class HomeTheaterFacade:
def __init__(self, amp, dvd, projector):
self.amp = amp
self.dvd = dvd
self.projector = projector
def watch_movie(self, movie):
print("Get ready to watch a movie...")
self.amp.on()
self.amp.set_volume(10)
self.dvd.on()
self.dvd.play(movie)
self.projector.on()
self.projector.wide_screen_mode()
print("Movie is playing")
def end_movie(self):
print("Shutting movie theater down...")
self.projector.off()
self.dvd.off()
self.amp.off()
print("Movie theater is off")
# Client code
def client_code():
amp = Amplifier()
dvd = DVDPlayer()
projector = Projector()
home_theater = HomeTheaterFacade(amp, dvd, projector)
home_theater.watch_movie("Inception")
print("\n")
home_theater.end_movie()
# Usage
client_code()
## Output
# Get ready to watch a movie...
# Amplifier is on
# Amplifier volume set to 10
# DVD Player is on
# DVD Player is playing Inception
# Projector is on
# Projector in widescreen mode
# Movie is playing
# Shutting movie theater down...
# Projector is off
# DVD Player is off
# Amplifier is off
# Movie theater is off