Skip to content

Commit 5b8e7bb

Browse files
authored
Merge pull request #12 from dancergraham/feature/facade
Add sample code for the facade design pattern
2 parents e0ef8d7 + 91b0369 commit 5b8e7bb

File tree

9 files changed

+298
-1
lines changed

9 files changed

+298
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ dmypy.json
130130
# Pyre type checker
131131
.pyre/
132132
.vscode/
133+
.idea/

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ I have added examples of pattern usage in the Python standard library and pypi -
1818
- [x] [Singleton](chapter05_singleton)
1919
- [x] [Command](chapter06_command)
2020
- [x] [Adapter](chapter07_adapter_facade)
21-
- [ ] [Façade](chapter07_adapter_facade)
21+
- [x] [Façade](chapter07_adapter_facade)
2222
- [x] [Template Method](chapter08_template)
2323
- [x] [Iterator](chapter09_iterator_composite)
2424
- [ ] [Composite](chapter09_iterator_composite)

chapter07_adapter_facade/__init__.py

Whitespace-only changes.

chapter07_adapter_facade/facade/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class HomeTheaterFacade:
2+
3+
def __init__(self, amp, tuner,
4+
player,
5+
projector,
6+
screen,
7+
lights,
8+
popper
9+
):
10+
self.amp = amp
11+
self.tuner = tuner
12+
self.player = player
13+
self.projector = projector
14+
self.screen = screen
15+
self.lights = lights
16+
self.popper = popper
17+
18+
def watch_movie(self, movie):
19+
print("Get ready to watch a movie...")
20+
self.popper.on()
21+
self.popper.pop()
22+
self.lights.dim(10)
23+
self.screen.down()
24+
self.projector.on()
25+
self.projector.wide_screen_mode()
26+
self.amp.on()
27+
self.amp.set_streaming_player(self.player)
28+
self.amp.set_surround_sound()
29+
self.amp.set_volume(5)
30+
self.player.on()
31+
self.player.play(movie)
32+
33+
def end_movie(self):
34+
print("Shutting movie theater down...")
35+
self.popper.off()
36+
self.lights.on()
37+
self.screen.up()
38+
self.projector.off()
39+
self.amp.off()
40+
self.player.stop()
41+
self.player.off()
42+
43+
def listen_to_radio(self, frequency):
44+
print("Tuning in the airwaves...")
45+
self.tuner.on()
46+
self.tuner.setFrequency(frequency)
47+
self.amp.on()
48+
self.amp.setVolume(5)
49+
self.amp.setTuner(self.tuner)
50+
51+
def end_radio(self):
52+
print("Shutting down the tuner...")
53+
self.tuner.off()
54+
self.amp.off()
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from facade.home_theatre_facade import HomeTheaterFacade
2+
from subcomponents.separates import Amplifier, Tuner, StreamingPlayer, Projector, Screen, TheaterLights, PopcornPopper
3+
4+
5+
def theatre_test_drive():
6+
amp = Amplifier("Amplifier")
7+
tuner = Tuner("AM/FM Tuner", amp)
8+
player = StreamingPlayer("Streaming Player", amp)
9+
# cd = CdPlayer("CD Player", amp)
10+
projector = Projector("Projector", player)
11+
lights = TheaterLights("Theater Ceiling Lights")
12+
screen = Screen("Theater Screen")
13+
popper = PopcornPopper("Popcorn Popper")
14+
15+
home_theater = HomeTheaterFacade(amp, tuner,
16+
player,
17+
projector,
18+
screen,
19+
lights,
20+
popper
21+
)
22+
23+
home_theater.watch_movie("Raiders of the Lost Ark")
24+
home_theater.end_movie()
25+
26+
27+
if __name__ == '__main__':
28+
theatre_test_drive()

chapter07_adapter_facade/readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ high-level cross-platform interface to methods and attributes from the `os`, `sy
2323

2424
## Running the code
2525

26+
### Adapter
2627
```bash
2728
python duck_adapter.py
2829
```
30+
31+
### Facade
32+
```bash
33+
python home_theatre.py
34+
```

chapter07_adapter_facade/subcomponents/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
class Amplifier:
2+
def __init__(self, description):
3+
self.description = description
4+
self.tuner = None
5+
self.player = None
6+
7+
def __str__(self):
8+
return self.description
9+
10+
def on(self):
11+
print(f"{self.description} on")
12+
13+
def off(self):
14+
print(f"{self.description} off")
15+
16+
def set_stereo_sound(self):
17+
print(f"{self.description} stereo mode on")
18+
19+
def set_surround_sound(self):
20+
print(f"{self.description} surround sound on (5 speakers, 1 subwoofer)")
21+
22+
def set_volume(self, level):
23+
print(f"{self.description} setting volume to {level}")
24+
25+
def set_tuner(self, tuner):
26+
print(f"{self.description} setting tuner to {tuner}")
27+
self.tuner = tuner
28+
29+
def set_streaming_player(self, player):
30+
print(f"{self.description} setting Streaming player to {player}")
31+
self.player = player
32+
33+
34+
class Tuner:
35+
def __init__(self, description, amplifier):
36+
self.description = description
37+
self.amplifier = amplifier
38+
self.frequency = None
39+
40+
def __str__(self):
41+
return self.description
42+
43+
def on(self):
44+
print(f"{self.description} on")
45+
46+
def off(self):
47+
print(f"{self.description} off")
48+
49+
def set_frequency(self, frequency):
50+
print(f"{self.description} setting frequency to frequency")
51+
self.frequency = frequency
52+
53+
def set_am(self):
54+
print(f"{self.description} setting AM mode")
55+
56+
def set_fm(self):
57+
print(f"{self.description} setting FM mode")
58+
59+
60+
class StreamingPlayer:
61+
def __init__(self, description, amplifier):
62+
self.description = description
63+
self.amplifier = amplifier
64+
self.current_chapter = 0
65+
self.movie = None
66+
67+
def __str__(self):
68+
return self.description
69+
70+
def on(self):
71+
print(f"{self.description} on")
72+
73+
def off(self):
74+
print(f"{self.description} off")
75+
76+
def play(self, arg):
77+
if isinstance(arg, str):
78+
self.movie = arg
79+
self.current_chapter = 0
80+
print(f'{self.description} playing "{self.movie}"')
81+
elif isinstance(arg, int):
82+
if self.movie is None:
83+
print(f"{self.description} can't play chapter {arg} no movie selected")
84+
else:
85+
self.current_chapter = arg
86+
print(
87+
f'{self.description} playing chapter {self.current_chapter} of "{self.movie}"'
88+
)
89+
90+
def stop(self):
91+
self.current_chapter = 0
92+
print(f'{self.description} stopped "{self.movie}"')
93+
94+
def pause(self):
95+
print(f'{self.description} paused "{self.movie}"')
96+
97+
def set_two_channel_audio(self):
98+
print(f"{self.description} set two channel audio")
99+
100+
def set_surround_audio(self):
101+
print(f"{self.description} set surround audio")
102+
103+
104+
class CdPlayer:
105+
def __init__(self, description, amplifier):
106+
self.description = description
107+
self.current_track = 0
108+
self.amplifier = amplifier
109+
self.title = None
110+
111+
def __str__(self):
112+
return self.description
113+
114+
def on(self):
115+
print(f"{self.description} on")
116+
117+
def off(self):
118+
print(f"{self.description} off")
119+
120+
def play(self, arg):
121+
if isinstance(arg, str):
122+
self.title = arg
123+
self.current_track = 0
124+
print(f'{self.description} playing "{self.title}"')
125+
elif isinstance(arg, int):
126+
if self.title is None:
127+
print(f"{self.description} can't play track {self.current_track}, no cd inserted")
128+
else:
129+
self.current_track = arg
130+
print(f'{self.description} playing track {self.current_track}')
131+
132+
def eject(self):
133+
print(f'{self.description} eject')
134+
135+
def stop(self):
136+
print(f'{self.description} stopped')
137+
138+
def pause(self):
139+
print(f'{self.description} paused "{self.title}"')
140+
141+
142+
class Projector:
143+
def __init__(self, description, player):
144+
self.description = description
145+
self.player = player
146+
147+
def __str__(self):
148+
return self.description
149+
150+
def on(self):
151+
print(f"{self.description} on")
152+
153+
def off(self):
154+
print(f"{self.description} off")
155+
156+
def wide_screen_mode(self):
157+
print(f"{self.description} in widescreen mode (16x9 aspect ratio)")
158+
159+
def tv_mode(self):
160+
print(f"{self.description} in tv mode (4x3 aspect ratio)")
161+
162+
163+
class Screen:
164+
def __init__(self, description):
165+
self.description = description
166+
167+
def __str__(self):
168+
return self.description
169+
170+
def up(self):
171+
print(f"{self.description} going up")
172+
173+
def down(self):
174+
print(f"{self.description} going down")
175+
176+
177+
class TheaterLights:
178+
def __init__(self, description):
179+
self.description = description
180+
181+
def __str__(self):
182+
return self.description
183+
184+
def on(self):
185+
print(f"{self.description} on")
186+
187+
def off(self):
188+
print(f"{self.description} off")
189+
190+
def dim(self, level):
191+
print(f"{self.description} dimming to {level} %")
192+
193+
194+
class PopcornPopper:
195+
def __init__(self, description):
196+
self.description = description
197+
198+
def __str__(self):
199+
return self.description
200+
201+
def on(self):
202+
print(f"{self.description} on")
203+
204+
def off(self):
205+
print(f"{self.description} off")
206+
207+
def pop(self):
208+
print(f"{self.description} popping popcorn!")

0 commit comments

Comments
 (0)