Real World Example
How do you turn on the computer? “Hit the power button” you say! That is what you believe because you are using a simple interface that computer provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade.
In Plain Words
Facade pattern provides a simplified interface to a complex subsystem.Wikipedia Definition
A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
Programmatic Example
Taking our computer example from above. Here we have the computer class:Key Participants
Facade
Facade
Provides a simple interface to the complex subsystem (in our example:
ComputerFacade)Subsystem Classes
Subsystem Classes
Implement subsystem functionality and handle work assigned by the Facade (in our example:
Computer and its methods)Client
Client
Uses the Facade instead of calling subsystem objects directly
Before and After Facade
Without Facade
With Facade
When to Use?
Use the Facade pattern when:
- You want to provide a simple interface to a complex subsystem
- There are many dependencies between clients and implementation classes
- You want to layer your subsystems
- You need to decouple a subsystem from clients and other subsystems
Benefits
- Simplicity: Provides a simple interface to a complex system
- Decoupling: Shields clients from subsystem components
- Layering: Helps structure your application into layers
- Easier Testing: Makes the subsystem easier to test
- Flexibility: You can still access subsystem classes directly if needed
Facade vs Direct Access
The Facade pattern doesn’t prevent you from accessing the subsystem classes directly. It simply provides a convenient higher-level interface. Clients can choose to use the facade or access subsystem classes directly based on their needs.
Real-World Applications
API Wrappers
Simplifying complex third-party APIs
Database Access
Hiding complex database operations
Framework Services
Laravel facades for accessing services
Payment Gateways
Simplifying payment processing APIs
Considerations
Example: Payment Processing Facade
Related Patterns
- Abstract Factory: Can be used with Facade to provide an interface for creating subsystem objects
- Mediator: Similar to Facade in that it abstracts functionality, but Mediator’s purpose is to abstract arbitrary communication between objects
- Singleton: Facade objects are often Singletons