Start learning free

LESSON 321 OF 343 · DESIGN PATTERNS

Facade Pattern

Practice Structural

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() has setLevel(n) and level(). The level goes from 0 to 100. New lights are at 100.
  • Projector() has on(), off() and isOn(). A new projector is off.
  • Speakers() has setVolume(v) and volume(). New speakers are at volume 0.

Write the facade, HomeTheater(lights, projector, speakers). It keeps a pointer 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. Then title is 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.
  • nowPlaying() 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 pass a pointer to each part. So the constructor takes a Lights*, a Projector* and a Speakers*.

The tests make the three parts themselves and give them to the theater. They keep their own pointers 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 title calls watch(title).
  • pause, resume and stop call the method of the same name.
  • status writes one line with four values. They are the lights' level(), the projector's isOn() as True or False, the speakers' volume() and the theater's nowPlaying().

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:

  • title has 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.

STEP 1 OF 25
    Ctrl + Enter to check