Facade Design Pattern
Ganesh Manchi
Full Stack @TCS ? | Ex-SDE @ZopSmart ?? ( Go | Java | React ) ??♂? | 1900+ Knight @Leetcode ?? | Open Source @ Layer5 ?? | CS Engineer ??
Alice and Bob are watching a movie at Bob's place, enjoying the latest action flick on Bob's impressive home theater system.
Alice: This movie night setup is amazing, Bob! You’ve really got all the gadgets.
Bob: Thanks, Alice! It did take some effort to get everything working together. Sometimes it feels like I need a degree just to turn on the TV and get the sound system right.
Alice: That’s actually a great segue into what I wanted to talk about today. Have you heard of the Facade Design Pattern?
Bob: Facade? No, what’s that about?
Alice: The Facade Design Pattern is a structural pattern that provides a simplified interface to a complex subsystem. It's like your remote control here, one simple interface to manage multiple complex devices.
Bob: Oh, that sounds useful! Can you break it down a bit more?
Alice: Sure thing! Think about your home theater system. You’ve got the TV, sound system, and DVD player, all with their own interfaces. Instead of turning each one on separately and setting them up, you could have a single interface to handle all of that for you.
Bob: I get it. So, it’s like creating a universal remote in code?
Alice: Exactly! Here, let me show you with some code:
领英推荐
// Subsystem classes
class TV {
void on() { System.out.println("TV is on"); }
void off() { System.out.println("TV is off"); }
}
class SoundSystem {
void on() { System.out.println("Sound system is on"); }
void off() { System.out.println("Sound system is off"); }
}
class DVDPlayer {
void on() { System.out.println("DVD player is on"); }
void off() { System.out.println("DVD player is off"); }
}
// Facade class
class HomeTheaterFacade {
private TV tv;
private SoundSystem soundSystem;
private DVDPlayer dvdPlayer;
public HomeTheaterFacade(TV tv, SoundSystem soundSystem, DVDPlayer dvdPlayer) {
this.tv = tv;
this.soundSystem = soundSystem;
this.dvdPlayer = dvdPlayer;
}
public void watchMovie() {
tv.on();
soundSystem.on();
dvdPlayer.on();
System.out.println("Movie is starting...");
}
public void endMovie() {
dvdPlayer.off();
soundSystem.off();
tv.off();
System.out.println("Movie has ended.");
}
}
class Main {
public static void main(String[] args) {
TV tv = new TV();
SoundSystem soundSystem = new SoundSystem();
DVDPlayer dvdPlayer = new DVDPlayer();
HomeTheaterFacade homeTheater = new HomeTheaterFacade(tv, soundSystem, dvdPlayer);
homeTheater.watchMovie();
homeTheater.endMovie();
}
}
Bob: So, with the HomeTheaterFacade class, I can start and stop the movie with just one call each?
Alice: Precisely! It makes your life easier by hiding the complexity of the subsystem behind a simple interface.
Bob: This is brilliant! No more fiddling with multiple remotes and settings. The Facade pattern not only simplifies the interaction but also makes the code cleaner and more maintainable.
Alice: Exactly. It’s particularly useful in managing complex systems where you want to provide a simple interface to the client.
Bob: Thanks, Alice. I’ll definitely look into applying this pattern to my projects. It sounds like a game changer.
Alice: Anytime, Bob. Now, let’s get back to our movie with your home theater, we’ve got the perfect setup!
Alice and Bob return to their movie, enjoying the seamless experience made possible by Bob's home theater system.
Until next time, signing off with ? Ganesh