Facade Pattern
The Facade pattern provides a simple, unified interface to a complex subsystem. Use it when a group of classes is hard to use directly and you want to expose only the high-level operations the rest of the application actually needs.
Overview
The Facade pattern hides a tangled subsystem behind a single, narrow API. Clients call one or two methods on the facade and never see the dozens of classes underneath; the facade orchestrates them. It is one of the cheapest patterns to apply and one of the most effective for taming legacy code or wrapping a heavy library you'd rather not expose to the rest of your codebase.
When to use it
- A subsystem has many moving parts but most callers only need a small subset of operations.
- You want to decouple the rest of the application from a complex or unstable internal API.
- You're refactoring legacy code and want a stable seam to migrate behind incrementally.
Example
class VideoFile { constructor(public name: string) {} }
class Codec { constructor(public type: string) {} }
class CodecFactory { extract(file: VideoFile) { return new Codec("mp4"); } }
class BitrateReader { static read(file: VideoFile, codec: Codec) { return "raw"; } }
class AudioMixer { fix(buffer: string) { return "fixed"; } }
class VideoConverter {
convert(filename: string, format: string): string {
const file = new VideoFile(filename);
const sourceCodec = new CodecFactory().extract(file);
const buffer = BitrateReader.read(file, sourceCodec);
const result = new AudioMixer().fix(buffer);
return `${result}.${format}`;
}
}
const converted = new VideoConverter().convert("clip.ogg", "mp4");Pros
- Drastically simplifies the API surface that clients see.
- Decouples client code from internal subsystem details.
- Great migration tool — refactor behind the facade without breaking callers.
Cons
- The facade can become a "god object" if you keep piling responsibilities into it.
- Hides flexibility: callers who need finer-grained control are forced to bypass it.
- Adds another class that must be kept in sync with the underlying subsystem.