---
name: facade
description: This skill should be used when the user asks about "facade pattern", "when to use facade", "implement facade", "simplify interface", "hide complexity", "wrapper for subsystem", or mentions needing a simplified interface to a complex subsystem.
version: 1.0.0
---

# Facade Pattern

## What Is It
The Facade pattern provides a unified, simplified interface to a complex subsystem of classes, libraries, or APIs. It defines a higher-level interface that makes the subsystem easier to use by hiding its complexity.

## When to Use
- **Complex library integration** - Provide simpler API over complex library
- **Legacy code wrapping** - Hide old code behind clean interface
- **Multi-step operations** - Coordinate multiple subsystem calls
- **Reducing coupling** - Client depends on facade, not many classes
- **Layered architectures** - Facade defines entry point to each layer
- **API simplification** - Reduce learning curve for new developers

## When NOT to Use
- **Simple subsystems** - No need if the existing interface is already simple
- **Need full control** - Facade limits access to subsystem features
- **Performance critical** - Facade adds a layer of indirection
- **Single client** - If only one client uses the subsystem

## How to Implement

### Implementation Steps
1. Identify the complex subsystem classes and their interactions
2. Create a facade class that holds references to subsystem objects
3. Implement high-level methods that delegate to subsystem classes
4. The facade orchestrates subsystem calls in the correct order
5. Clients interact only with the facade, not subsystem directly

### TypeScript Implementation

```typescript
// Complex subsystems
class CPU {
  freeze() { console.log('Freezing CPU'); }
  jump(position: number) { console.log(`Jumping to ${position}`); }
  execute() { console.log('Executing'); }
}

class Memory {
  load(position: number, data: string) { console.log(`Loading ${data} at ${position}`); }
}

class HardDrive {
  read(lba: number, size: number): string { return 'data'; }
}

// Facade
class ComputerFacade {
  private cpu = new CPU();
  private memory = new Memory();
  private hardDrive = new HardDrive();

  start() {
    this.cpu.freeze();
    const data = this.hardDrive.read(0, 1024);
    this.memory.load(0, data);
    this.cpu.jump(0);
    this.cpu.execute();
  }
}

// Usage
const computer = new ComputerFacade();
computer.start(); // Simple!
```

## Code Examples
See `examples/` directory for runnable TypeScript implementations:
- `examples/facade.ts` - Basic implementation with home theater system
- `examples/facade-advanced.ts` - Real-world example with e-commerce checkout

## Related Patterns
- **Adapter** - Adapter changes interface; Facade simplifies existing interface
- **Decorator** - Decorator adds functionality; Facade simplifies interface
- **Mediator** - Mediator abstracts communication; Facade abstracts interface
- **Abstract Factory** - Can be used with Facade to hide subsystem creation
