Learn Facade Design Pattern through stories

Tram Ho

What is Design Pattern

Home cinema

Before starting to learn about Facade Pattern, we will consider how to build home theater application.

alt text

As you can see, this is a complex system, lots of classes and lots of interactions between classes.

The system includes projector, audio, DVD, CD, popcorn maker, lamp, etc.

It took you weeks to set up, connect, and integrate the devices … And now start enjoying what movies.

Watch movies in hell

Select a DVD movie disc, and enjoy, … But before watching the movie you have to do the following tasks:

  1. Start the popcorn making machine.
  2. Press the button to make popcorn
  3. Turn the lights back on.
  4. Lower the screen.
  5. Turn on the projector.
  6. Install the projector port into DVD.
  7. Switch the projector status to wide-screen.
  8. Connect the stereo (amplifier) ​​to the DVD.
  9. Switch the sound mode to surround sound.
  10. Adjust the sound to medium mode (5).
  11. Open DVD player.
  12. Click the Play button to start watching movies.

And here is the code you need to execute:

But not over here …

  • When the movie ends how do you turn everything off? Maybe we did the same thing again?
  • Is it easy when we want to listen to a CD or radio?
  • If you want to upgrade your system, then you must learn how each DVD, CD, radio, etc. works.

What should we do? A complicated cinema system has appeared, we have to buy them but we have to serve them in reverse ..

Let’s take a look at Facade Patterns that will help you get out of this predicament.

Facade pattern

Facade (superficial, main face) -> It means that you only need one representative class to perform all the complex tasks of the subsystem (DVD, CD, radio, ..).

Do not worry too much, if you want to build the functionality of the subsystem, because the functions of the subsystem are still there, you can use as you like.

But if you want a system to directly execute cumbersome tasks, Facade will help you.

And this is the overall picture of Facade’s operating model

alt text

  1. First we create the HomeTheaterFacade class to implement the main function like watchMovie()
  2. Facade class views the Amplifier, DvdPlayer, CdPlayer, Tuner, etc. as a subsystem, and calls subsystems to execute the watchMovie () function.
  3. Now we only need to execute the functions of Facade, and no need to execute functions in the subsystem. Now watching movies for us is easy, just call watchMovie () and we can watch movies right away.
  4. With Facade design you still have direct access to the subsystem, to perform advanced functions (adjust the brightness of the lights, adjust the increase / decrease the sound, …).

Building smart theaters

The first step is to build Facade using compositions, to access subsystem components:

And let’s start watching movies

Results displayed:

Get ready to watch a movie …

Popcorn Popper on

Popcorn Popper popping popcorn!

Theater Ceiling Lights dimming to 10%

Theater Screen going down

Top-O-Line Projector on

Top-O-Line Projector in widescreen mode (16×9 aspect ratio)

Top-O-Line Amplifier on

Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player

Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer)

Top-O-Line Amplifier setting volume to 5

Top-O-Line DVD Player on

Top-O-Line DVD Player playing “Raiders of the Lost Ark”

Shutting movie theater down …

Popcorn Popper off

Theater Ceiling Lights on

Theater Screen going up

Top-O-Line Projector off

Top-O-Line Amplifier off

Top-O-Line DVD Player stopped “Raiders of the Lost Ark”

Top-O-Line DVD Player eject

Top-O-Line DVD Player off

Definition of Facade Pattern

The Facade Pattern provides a unified interface to a set of interfaces in a subsytem. Facade defines a higher-level interface that makes the subsystem easier to use.

Provisional translation is:

Facade Pattern provides a unified interface for subsystems. Facade defines a high-level interface to make it easy to use subsystems.

Overall pattern Facade Pattern

alt text

Epilogue:

With Facade Pattern you can do the following:

  1. Facade can be used to execute a wide range of subsystem commands. In case you want to customize a function of the subsystem, you can still access it directly.
  2. Besides the function of grouping subsystems, Facade also has the function of checking, executing orders intelligently, such as: to watch DVD, you must open the DVD first.
  3. In the example above we only have 1 Facade, but in reality you can add as many Facades as you want, depending on the case.
  4. Facade also helps separate client code and executable code, when changes, you only need to modify the Facade code without affecting the client. (Note, 1 Facade can be used by multiple clients)

Source code: https://github.com/bethrobson/Head-First-Design-Patterns/tree/master/src/headfirst/designpatterns/facade/hometheater

Contribute

Please give me a minute to help me. Please leave your comments to help the latter to read and understand better.

Thank you for your interest in this article. I wish you a good day! ?

References from: Head First Design Pattern (Eric Freeman & Elisabeth Freeman)

Related posts:

What is Design Pattern

Share the news now

Source : Viblo