The Facade Pattern belongs to the Structural category of design patterns.
Why? Because it hides complex subsystems behind a simple interface, making the client’s life easier.
Say you are travelling to your favorite holiday destination. You enter the airport an walk to the check-in counter. You would not directly deal with baggage handling, seat assignment, boarding pass generation etc. Instead, you walk up to one counter, hand over your passport, and the staff takes care of all the background steps by coordinating with multiple systems.
That is the Facade Pattern in action. You can think of it as a single, simplified entry point to a complex set of operations.
The Smart Home
Imagine you have a smart home with many subsystems like Lights, Air Conditioner, Security Camera, Music System.
Say its Friday and you decide to have a movie night. You would want to have the stage set for it to have a wonderful experience.
Without Facade you would have to, talk to each subsystem directly - lights, AC, TV, speakers. The client code becomes a to-do list of commands and is tightly coupled to the subsystem details. Any small change (say, new speakers) forces changes everywhere.
With Facade, you just call one method - "StartMovieNight", and its all set.
What Does the Code Look Like?
// Step 1: Subsystems
Class Lights
Method TurnOn()
Method Dim()
Class AirConditioner
Method TurnOn()
Method SetTemperature(temp)
Class TV
Method TurnOn()
Method SetInput(input)
Class Speakers
Method TurnOn()
Method SetMode(mode)
// Step 2: Facade
Class HomeTheaterFacade
Property lights
Property ac
Property tv
Property speakers
Constructor(lights, ac, tv, speakers)
this.lights = lights
this.ac = ac
this.tv = tv
this.speakers = speakers
Method StartMovieNight()
lights.Dim()
ac.TurnOn()
ac.SetTemperature(22)
tv.TurnOn()
tv.SetInput("HDMI")
speakers.TurnOn()
speakers.SetMode("Theater")
// caller logic
lights = new Lights()
ac = new AirConditioner()
tv = new TV()
speakers = new Speakers()
homeTheater = new HomeTheaterFacade(lights, ac, tv, speakers)
homeTheater.StartMovieNight()
We define individual subsystem classes (Lights, AC, TV, Speakers), with each having its own detailed operations.
Instead of the client calling these classes directly, we introduce HomeTheaterFacade.
The facade exposes a single method StartMovieNight()
, which internally coordinates all the subsystem calls in the right sequence.
This makes the client’s job super easy. Its just one call that does it all for you.
What Did We Achieve?
- Simplicity: Clients deal with one simple interface instead of multiple complex subsystems.
- Loose Coupling: Clients aren not tied to subsystem details. If internals change, the facade shields them.
- Reusability: Common workflows that have complex subsystems synchronized can be packaged into facade methods.
When Should You Use the Facade Pattern?
- When your system has multiple subsystems that are complex or frequently change.
- When you want to provide a simplified entry point for common tasks.
- When you want to reduce dependencies between client code and low level operations.
Use Cases?
- Holiday booking websites : one interface coordinates flights, hotels, cars, activities
- Payment gateways : single call hides multiple banking/payment subsystems
- Libraries/Frameworks : where a facade API wraps complex underlying logic
To summarize, it would be apt to say,
The Facade Pattern is like your airport check-in counter, one simple stop that takes care of a whole set of complex behind the scenes processes. Hiding the complexities and getting the work done with just a single entry point.
Hope this gave a good idea on the Facade Pattern.
Next up: Interpreter Pattern. Stay tuned!
BONUS: Relation to Adapter & Mediator
Facade is often confused with two patterns - Adapter & Mediator. Here is a quick comparison between the three.
Adapter : It focuses on compatibility. Makes two incompatible interfaces work together. Example, plugging a US charger into an EU socket.
Facade : It focuses on simplicity. Hides a complex subsystem behind a clean, unified interface.
Mediator : It focuses on communication. Defines how objects interact with each other without them referring to each other directly.
Top comments (0)