Facade
structural
Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Intent
The Facade pattern provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use.
Motivation
When you want to provide a simple interface to a complex subsystem, or you want to layer your subsystems, the Facade pattern is useful. It shields clients from subsystem components.
Structure
The pattern involves a Facade class that provides simple methods to complex subsystem functionality. The Facade delegates client requests to appropriate subsystem objects.
Participants
- •Facade - knows which subsystem classes are responsible for a request and delegates client requests
- •Subsystem classes - implement subsystem functionality and handle work assigned by the Facade
Pros
- ✓Shields clients from subsystem components
- ✓Promotes weak coupling
- ✓Simplifies the interface
- ✓Allows for easier testing
Cons
- ✗Can become a god object
- ✗May limit access to advanced features
Use Cases
Complex library APIs
Home theater systems
Compiler subsystems
Database connection management
Video conversion frameworks
Implementation Tips
Considerations
- •Decide what functionality to expose
- •Consider making facade optional
- •Think about subsystem initialization
- •Plan for subsystem evolution
Tips
- •Keep the facade interface simple and focused
- •Don't prevent access to subsystem if needed
- •Consider using facade as a singleton
- •Document what the facade simplifies