# Purpose
The Facade pattern provides a unified and simplified interface to a set of interfaces in a subsystem.
It facilitates the use of large and complex systems for clients.
# Problem
The complexity associated with systems with multiple interdependent components or subsystems with their own interfaces.
These systems are difficult to understand, generate coupling, and make changes difficult.
# Solution
The solution proposed by the Facade is:
-
Unified Interface: Simplifies interaction with the subsystem.
-
Coupling Reduction: Clients don't need to know the inner details.
# Structure
# Participants
- Facade: Delegates requests to appropriate objects in the subsystem.
- Subsystems Class: Implements the subsystem functionality.
- Client: Accesses the subsystem through the Facade.
# When to Use It
-
To provide a simple interface to a complex subsystem.
-
When there are many dependencies between clients and implementation classes.
# Advantages
-
verified
Simplicity: Facilitates subsystem usage.
-
verified
Abstraction: Hides internal complexity.
-
verified
Decoupling: Reduces direct dependencies.
# Disadvantages
-
warning
God Object Risk: Too much functionality in a single class.
-
warning
Limitations: Might hide needed advanced features.
# Example: Home Theater
We are developing an integrated system consisting of TV, DVD/BluRay, Surround Sound, etc. Each one with its own interface.
# Java Code
class HomeTheaterFacade {
private Television tv;
private SoundSystem sound;
private BluRayPlayer bluRay;
public void watchMovie() {
tv.turnOn();
sound.turnOn();
bluRay.turnOn();
bluRay.play();
}
}
public class Client {
public static void main(String[] args) {
HomeTheaterFacade facade = new HomeTheaterFacade(tv, sound, bluRay);
facade.watchMovie();
}
}
# Mapping Participants
- Television, Sound, BluRay (Subsystems): Internal classes.
- HomeTheaterFacade (Facade): Simplified interface.
- Client: Uses the Facade.
# Conclusions
The Facade pattern vastly simplifies user interaction. If we add another device, we only need to modify the Facade without affecting clients.
# Related Patterns
Abstract Factory
Can be used with Facade to build subsystems.
Singleton
Sometimes Facade is implemented as a Singleton.
Mediator
Abstracts functionality of existing classes, but centralizes communication.