> For the complete documentation index, see [llms.txt](https://al-mamun.gitbook.io/al-mamun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://al-mamun.gitbook.io/al-mamun/design-patterns/facade-pattern.md).

# Facade Pattern

The **Facade Pattern** is a **structural design pattern** that provides a **simple interface** to a complex system.

Instead of interacting with multiple classes directly, the client communicates with a single facade class.

***

## Real Life Example

Think about an **Online Food Order App** 🍔

To place an order internally, the system may:

1. Validate payment
2. Check restaurant availability
3. Assign delivery rider
4. Generate invoice
5. Send notification

But the customer only clicks:

```
Place Order
```

The app hides all complexities behind one simple interface.

This is the Facade Pattern.

***

## Structure

```
Client
   ↓
Facade
   ↓
Subsystem Classes
```

***

## Laravel / PHP Example

### Scenario

We are building an eCommerce order system.

Complex subsystems:

* Payment Service
* Inventory Service
* Shipping Service
* Notification Service

We simplify everything using a facade.

***

## Step 1: Subsystem Classes

### Payment Service

```
class PaymentService
{
    public function processPayment()
    {
        return "Payment processed";
    }
}
```

***

### Inventory Service

```
class InventoryService
{
    public function updateStock()
    {
        return "Stock updated";
    }
}
```

***

### Shipping Service

```
class ShippingService
{
    public function createShipment()
    {
        return "Shipment created";
    }
}
```

***

### Notification Service

```
class NotificationService
{
    public function sendNotification()
    {
        return "Notification sent";
    }
}
```

***

## Step 2: Create Facade Class

```
class OrderFacade
{
    protected PaymentService $payment;
    protected InventoryService $inventory;
    protected ShippingService $shipping;
    protected NotificationService $notification;

    public function __construct()
    {
        $this->payment = new PaymentService();
        $this->inventory = new InventoryService();
        $this->shipping = new ShippingService();
        $this->notification = new NotificationService();
    }

    public function placeOrder()
    {
        return [
            $this->payment->processPayment(),
            $this->inventory->updateStock(),
            $this->shipping->createShipment(),
            $this->notification->sendNotification(),
        ];
    }
}
```

***

## Step 3: Usage

```
$order = new OrderFacade();

print_r($order->placeOrder());
```

***

## Output

```
Array
(
    [0] => Payment processed
    [1] => Stock updated
    [2] => Shipment created
    [3] => Notification sent
)
```

***

## Without Facade

Client must handle everything manually:

```
$payment = new PaymentService();
$inventory = new InventoryService();
$shipping = new ShippingService();
$notification = new NotificationService();

$payment->processPayment();
$inventory->updateStock();
$shipping->createShipment();
$notification->sendNotification();
```

Too much complexity.

***

## With Facade

```
$orderFacade->placeOrder();
```

Simple and clean.

***

## Advantages

### 1. Simplifies Complex Systems

One interface for multiple services.

***

### 2. Reduces Coupling

Client does not depend on subsystem details.

***

### 3. Cleaner Code

Improves readability and maintainability.

***

### 4. Easy Integration

Useful for:

* APIs
* Third-party libraries
* Large systems

***

## Real Laravel Example

Laravel itself heavily uses the Facade Pattern.

Examples:

| Laravel Facade | Underlying Service     |
| -------------- | ---------------------- |
| `Cache::get()` | Cache Manager          |
| `DB::table()`  | Database Manager       |
| `Mail::send()` | Mail Service           |
| `Auth::user()` | Authentication Service |

***

## Example

```
Cache::put('name', 'Sabuj');
```

Behind the scenes, Laravel resolves multiple internal classes.

That is Facade Pattern.

***

## Facade vs Adapter

| Facade                     | Adapter                                  |
| -------------------------- | ---------------------------------------- |
| Simplifies complex system  | Converts one interface to another        |
| Focus on usability         | Focus on compatibility                   |
| Provides unified interface | Makes incompatible classes work together |

***

## Facade vs Decorator

| Facade                    | Decorator              |
| ------------------------- | ---------------------- |
| Simplifies access         | Adds behavior          |
| Hides complexity          | Enhances object        |
| Structural simplification | Functional enhancement |

***

## When to Use Facade Pattern

Use Facade when:

✅ System is complex\
✅ Multiple subsystems exist\
✅ You want cleaner APIs\
✅ You want to reduce client dependency

***

## Short Interview Definition

> The Facade Pattern provides a simplified unified interface to a complex subsystem.
