Facade
Provides a simplified interface to a complex subsystem.
Understanding Facade
The Facade pattern provides a simplified, unified interface to a complex subsystem of classes, libraries, or frameworks. Rather than exposing the subsystem's intricate API to client code, the facade wraps the complexity behind a single, easy-to-use method. In Go, facades are typically structs that aggregate multiple subsystem components and provide high-level methods that orchestrate their interactions — like an order processing facade that coordinates inventory, payment, and shipping behind a single PlaceOrder() call.
Key Concepts
- •Simplified interface — provides a few high-level methods instead of exposing dozens of subsystem APIs
- •Subsystem orchestration — the facade coordinates multiple subsystem components to fulfill complex operations
- •Not a restriction — clients can still access subsystem components directly for advanced use cases
- •Dependency reduction — client code depends only on the facade, not on the subsystem classes
When to Use
- • A subsystem has grown complex with many interacting components
- • You want to layer your system with a clean entry point for each layer
- • Client code needs to perform multi-step operations across multiple services
- • You want to reduce coupling to third-party library internals
- • The facade becomes a "god object" that does too much
- • The subsystem is simple enough that direct usage is clearer
- • You need fine-grained control that the facade hides
Structure
How It Works
Complex Subsystem
Multiple interconnected components with complex interfaces.
Basic Implementation
Computer startup facade hiding complex boot sequence:
Real-World Example: E-commerce Order
Order processing facade coordinating inventory, payment, and shipping: