Facade
Provide a simple, focused interface over a complex subsystem β clients call the Facade and don't need to understand what's happening under the hood.
Intent & Description
π― Intent
Hide subsystem complexity behind a simple interface so clients only deal with what they actually need.
π Context
You’re integrating a complex library β a video processing pipeline, a cloud storage SDK, a payment processing system β that has dozens of classes and initialization steps. Client code shouldn’t need to know about all of that.
π‘ Solution
Create a Facade class that exposes a simple, high-level API covering the operations clients actually need. Internally it orchestrates the complex subsystem. Clients use the Facade; power users who need more control can still access the subsystem directly.
Real-world Use Case
Source
π TL;DR
Simple API in front of complex internals. Client calls one method, Facade orchestrates the subsystem. Keep it focused or it becomes a God Object that owns everything.
Advantages
- Clients are shielded from subsystem complexity β just call the Facade.
Disadvantages
- Facade can accumulate responsibilities and become a God Object that knows too much about the entire system.
class CPU {
freeze() { console.log("CPU Freezing"); }
jump(position) { console.log(`Jumping to ${position}`); }
execute() { console.log("Executing..."); }
}
class Memory {
load(position, data) { console.log(`Loading ${data} to ${position}`); }
}
class ComputerFacade {
constructor() {
this.cpu = new CPU();
this.memory = new Memory();
}
start() {
this.cpu.freeze();
this.memory.load(0x00, "OS Boot Sector");
this.cpu.jump(0x00);
this.cpu.execute();
}
}