Facade

Provide simplified interface to complex subsystem

Overview

The Facade pattern provides a simplified interface to a complex subsystem. It hides the complexity of the subsystem and provides a single, easy-to-use interface.

Facade doesn't add functionality - it just simplifies access to existing functionality.

Key Concepts

Simplified interface to complex subsystem

Hides complexity from clients

Single entry point to subsystem

Decouples clients from subsystem

Doesn't prevent direct subsystem access

Code Example

java
1// Complex subsystem classes
2class Inventory {
3    public boolean checkStock(String productId) {
4        System.out.println("Checking inventory for: " + productId);
5        return true;
6    }
7    
8    public void reserveItem(String productId) {
9        System.out.println("Reserving item: " + productId);
10    }
11}
12
13class PaymentProcessor {
14    public boolean validateCard(String cardNumber) {
15        System.out.println("Validating card: " + cardNumber);
16        return true;
17    }
18    
19    public boolean chargeCard(String cardNumber, double amount) {
20        System.out.println("Charging $" + amount + " to card");
21        return true;
22    }
23}
24
25class ShippingService {
26    public String calculateShipping(String address) {
27        System.out.println("Calculating shipping to: " + address);
28        return "3-5 business days";
29    }
30    
31    public void scheduleDelivery(String orderId, String address) {
32        System.out.println("Scheduling delivery for order: " + orderId);
33    }
34}
35
36class NotificationService {
37    public void sendEmail(String email, String message) {
38        System.out.println("Sending email to: " + email);
39    }
40    
41    public void sendSMS(String phone, String message) {
42        System.out.println("Sending SMS to: " + phone);
43    }
44}
45
46// Facade - simplified interface
47class OrderFacade {
48    private Inventory inventory;
49    private PaymentProcessor payment;
50    private ShippingService shipping;
51    private NotificationService notification;
52    
53    public OrderFacade() {
54        this.inventory = new Inventory();
55        this.payment = new PaymentProcessor();
56        this.shipping = new ShippingService();
57        this.notification = new NotificationService();
58    }
59    
60    // Simple method that orchestrates complex operations
61    public boolean placeOrder(String productId, String cardNumber, 
62                              double amount, String address, String email) {
63        System.out.println("\n=== Processing Order ===");
64        
65        // Step 1: Check inventory
66        if (!inventory.checkStock(productId)) {
67            notification.sendEmail(email, "Product out of stock");
68            return false;
69        }
70        
71        // Step 2: Process payment
72        if (!payment.validateCard(cardNumber)) {
73            notification.sendEmail(email, "Invalid card");
74            return false;
75        }
76        
77        if (!payment.chargeCard(cardNumber, amount)) {
78            notification.sendEmail(email, "Payment failed");
79            return false;
80        }
81        
82        // Step 3: Reserve item
83        inventory.reserveItem(productId);
84        
85        // Step 4: Schedule shipping
86        String orderId = "ORD-" + System.currentTimeMillis();
87        shipping.scheduleDelivery(orderId, address);
88        
89        // Step 5: Notify customer
90        notification.sendEmail(email, "Order " + orderId + " confirmed!");
91        
92        System.out.println("=== Order Complete ===\n");
93        return true;
94    }
95}
96
97// Usage - client only needs to know about Facade
98public class Main {
99    public static void main(String[] args) {
100        OrderFacade orderFacade = new OrderFacade();
101        
102        // One simple call instead of managing 5 subsystems
103        orderFacade.placeOrder(
104            "PROD-123",
105            "1234-5678-9012-3456",
106            99.99,
107            "123 Main St",
108            "customer@email.com"
109        );
110    }
111}

OrderFacade simplifies placing an order by coordinating inventory, payment, shipping, and notification subsystems.

When to Use

  • When you want to simplify a complex subsystem

  • To provide a simple interface to clients

  • To decouple clients from subsystem implementation

  • When layering subsystems

Real-World Example

  • Computer startup (one power button hides BIOS, OS loading, etc.)
  • Hotel concierge (one person for all services)
  • JDBC driver (simplified database access)
  • Compiler (one compile command hides lexing, parsing, optimization)

💡 Interview Tips

  • Explain it simplifies, doesn't add functionality

  • Show e-commerce checkout example

  • Compare with Adapter (simplify vs convert)

  • Mention it's commonly used in layered architectures

  • Discuss when NOT to use (when simplicity not needed)

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Facade - Structural Patterns | LLD | Revise Algo