Some jobs need several parts that are fiddly to use together. Each part has its own methods, and they must be called in the right order.
The Facade pattern puts one simple class in front of the parts. It offers the few actions people want. Each action makes the right calls on the parts for you.
Here you write the one-button remote of a home theater. It has three parts, each from another maker's library.
The libraries are already in the code, and you must not change them:
Lights()hasset_level(n)andlevel(). The level goes from 0 to 100. New lights are at 100.Projector()hason(),off()andis_on(). A new projector is off.Speakers()hasset_volume(v)andvolume(). New speakers are at volume 0.
Write the facade, HomeTheater(lights, projector, speakers). It keeps a reference to each part it gets. So it works on those very parts, not on copies.
Its methods:
watch(title)dims the lights to 10. It turns the projector on and sets the volume to 25. Thentitleis the movie playing. It replaces any movie before it.pause()works only while a movie plays and isn't paused. It sets the lights to 50. The movie stays the one playing.resume()works only while a movie is paused. It sets the lights back to 10.stop()sets the lights to 100, turns the projector off and sets the volume to 0. Then nothing plays.now_playing()returns the title of the movie playing, or"none"when nothing plays.
A watch while a movie is paused starts the new movie, not paused. A pause or resume that doesn't work changes nothing.
The tests make the three parts themselves and give them to the theater. They keep their own references and read the parts after the commands. So the theater must work on the very parts it was given.
Each example runs commands on a new theater, one per line:
watch titlecallswatch(title).pause,resumeandstopcall the method of the same name.statuswrites one line with four values. They are the lights'level(), the projector'sis_on()asTrueorFalse, the speakers'volume()and the theater'snow_playing().
Only status writes. For example, a status line can be 10 True 25 jaws.
Example 1:
Input: 1. status 2. pause 3. resume 4. status 5. watch jaws 6. pause 7. status 8. resume 9. status 10. stop 11. status Output: 100 False 0 none 100 False 0 none 50 True 25 jaws 10 True 25 jaws 100 False 0 none Explanation: The parts start as new. The lights are at 100. The projector is off, and the volume is 0. Nothing plays, so commands 2 and 3 change nothing. Command 5 dims the lights to 10 and turns the projector on. It also sets the volume to 25. Command 6 pauses jaws, and the lights go up to 50. Command 8 resumes it, and the lights go back to 10. Command 10 stops it. Every part is as new again.
Example 2:
Input: 1. watch up 2. resume 3. status 4. pause 5. pause 6. status 7. watch coco 8. status 9. pause 10. status 11. stop 12. pause 13. resume 14. status Output: 10 True 25 up 50 True 25 up 10 True 25 coco 50 True 25 coco 100 False 0 none Explanation: Command 2 finds no paused movie, so it changes nothing. Command 4 pauses up. Command 5 changes nothing. Command 7 starts coco while up is paused. The lights go back to 10, and coco is not paused. So command 9 can pause it. After the stop, commands 12 and 13 change nothing.
Constraints:
titlehas 1 to 20 characters.- Each character is a lowercase letter or a digit.
- No title is
none. - There are at most 100,000 commands in all.
Note:
Each call should run in O(1) time.
Free plan: 30 tutor messages. PRO is unlimited.
