> For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt.

# Facade

## Overview

Provide a simplified interface to a complex subsystem.

## When to use

- You want a clean API for a complex set of classes.
- You want to decouple clients from subsystem details.

## Java example

```java
class PaymentFacade {
    private final FraudCheck fraud = new FraudCheck();
    private final PaymentGateway gateway = new PaymentGateway();

    public boolean pay(String userId, int amount) {
        if (!fraud.isAllowed(userId, amount)) {
            return false;
        }
        return gateway.charge(userId, amount);
    }
}
```

## TypeScript example

```ts
class PaymentFacade {
  private fraud = new FraudCheck();
  private gateway = new PaymentGateway();

  pay(userId: string, amount: number): boolean {
    if (!this.fraud.isAllowed(userId, amount)) {
      return false;
    }
    return this.gateway.charge(userId, amount);
  }
}
```

## Pros and cons

Pros:

- Simplifies client usage.
- Reduces coupling.

Cons:

- Can become a god object.
- May hide important capabilities.

## Common pitfalls

- Putting business logic into the facade.
- Growing the facade instead of creating focused APIs.

## Related patterns

- [Adapter](https://nhanbe.id.vn/design-patterns/structural/adapter.md)
- [Singleton](https://nhanbe.id.vn/design-patterns/creational/singleton.md)
