Home cinema
Before starting to learn about Facade Pattern, we will consider how to build home theater application.
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:
- Start the popcorn making machine.
- Press the button to make popcorn
- Turn the lights back on.
- Lower the screen.
- Turn on the projector.
- Install the projector port into DVD.
- Switch the projector status to wide-screen.
- Connect the stereo (amplifier) to the DVD.
- Switch the sound mode to surround sound.
- Adjust the sound to medium mode (5).
- Open DVD player.
- Click the Play button to start watching movies.
And here is the code you need to execute:
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 | //Mở máy bắp rang bơ và tiến hành tạo bắp popper.on(); popper.pop(); //Chỉnh độ sáng lại còn 10% lights.dim(10); //Hạ màn máy chiếu xuống screen.dowm(); //Bật máy chiếu và chuyển sang chế độ wideScreen projector.on(); projector.setInput(dvd); projector.wideScreenMode(); //Mở máy amplifier, kết nối với DVD và cài đặt âm thanh 5 amp.on(); amp.setDvd(dvd); amp.setSurroundSound(); amp.setVolume(5); //Cuối cùng là mở đầu DVD và xem phim dvd.on(); dvd.play(movie); |
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
- First we create the
HomeTheaterFacade
class to implement the main function likewatchMovie()
- Facade class views the Amplifier, DvdPlayer, CdPlayer, Tuner, etc. as a subsystem, and calls subsystems to execute the watchMovie () function.
- 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.
- 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:
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 89 90 91 92 93 94 95 96 | public class HomeTheaterFacade { Amplifier amp; Tuner tuner; DvdPlayer dvd; CdPlayer cd; Projector projector; TheaterLights lights; Screen screen; PopcornPopper popper; //Facade truyền những phần tử của hệ thống con vào constructor public HomeTheaterFacade(Amplifier amp, Tuner tuner, DvdPlayer dvd, CdPlayer cd, Projector projector, Screen screen, TheaterLights lights, PopcornPopper popper) { this.amp = amp; this.tuner = tuner; this.dvd = dvd; this.cd = cd; this.projector = projector; this.screen = screen; this.lights = lights; this.popper = popper; } //Hàm xem phim thực thi tuần tự từng bước như ta đã xét ở trước, //Mỗi nhiệm vụ sẽ được thực thi bới các hệ thống con tương ứng. public void watchMovie(String movie) { System.out.println("Get ready to watch a movie..."); popper.on(); popper.pop(); lights.dim(10); screen.down(); projector.on(); projector.wideScreenMode(); amp.on(); amp.setDvd(dvd); amp.setSurroundSound(); amp.setVolume(5); dvd.on(); dvd.play(movie); } //Cách hoạt động cũng tương tự như hàm watchMovie nhưng theo hướng ngược lại public void endMovie() { System.out.println("Shutting movie theater down..."); popper.off(); lights.on(); screen.up(); projector.off(); amp.off(); dvd.stop(); dvd.eject(); dvd.off(); } public void listenToCd(String cdTitle) { System.out.println("Get ready for an audiopile experence..."); lights.on(); amp.on(); amp.setVolume(5); amp.setCd(cd); amp.setStereoSound(); cd.on(); cd.play(cdTitle); } public void endCd() { System.out.println("Shutting down CD..."); amp.off(); amp.setCd(cd); cd.eject(); cd.off(); } public void listenToRadio(double frequency) { System.out.println("Tuning in the airwaves..."); tuner.on(); tuner.setFrequency(frequency); amp.on(); amp.setVolume(5); amp.setTuner(tuner); } public void endRadio() { System.out.println("Shutting down the tuner..."); tuner.off(); amp.off(); } } |
And let’s start watching movies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class HomeTheaterTestDrive { public static void main(String[] args) { //Khởi tạo các hệ thống con Amplifier amp = new Amplifier("Top-O-Line Amplifier"); Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp); DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp); CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp); Projector projector = new Projector("Top-O-Line Projector", dvd); TheaterLights lights = new TheaterLights("Theater Ceiling Lights"); Screen screen = new Screen("Theater Screen"); PopcornPopper popper = new PopcornPopper("Popcorn Popper"); Tích hợp hệ thống con HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, projector, screen, lights, popper); //Sử dụng hệ thống homeTheater.watchMovie("Raiders of the Lost Ark"); homeTheater.endMovie(); } } |
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
Epilogue:
With Facade Pattern you can do the following:
- 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.
- 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.
- 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.
- 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)