Facade
Category
Structural pattern
Problem
Provide a simplified interface to a library, a framework, or any other complex set of classes. When a system is very complex or difficult to use, a facade can provide a simple entry point.
Solution
- Create a single class (the facade) that wraps the complex subsystem
- The facade provides simplified methods that coordinate the underlying complexity
- Clients interact only with the facade, not the subsystem classes directly
Structure
- Facade: Knows which subsystem classes are involved; forwards requests to them
- Subsystem classes: Implement the subsystem functionality
- Client: Uses the facade to access subsystem functionality
Key Benefits
- Simplified interface — Clients don’t need to understand the subsystem complexity
- Loose coupling — Reduces dependencies between clients and subsystem
- Better organization — Can serve as a high-level interface to a large codebase
Trade-offs
- Can become a god object if it tries to simplify too many subsystems
- May encourage clients to bypass it, defeating the purpose
When to Use
- 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 to reduce coupling