Facade Pattern

Intent
Problem
- Many interdependent classes
- Initialization order
- Data transformations This couples business logic tightly to low-level details, making it hard to maintain.
Solution
- Offers a simplified interface
- Manages interactions with the subsystem
- Keeps client code clean and decoupled
Real-World Analogy
Structure
- Client: Calls the Facade
- Facade: Provides simple methods
- Subsystem Classes: Do the real work (but hidden from the client)
Example (JavaScript-Inspired Pseudocode)
Javascript
// Subsystem classes (complex and low-level)
class VideoFile {
constructor(name) {
this.name = name;
}
}
class CodecFactory {
extract(file) {
return file.name.endsWith(".mp4") ? new MPEG4Codec() : new OggCodec();
}
}
class MPEG4Codec {}
class OggCodec {}
class BitrateReader {
static read(filename, codec) {
console.log(`Reading file ${filename} with ${codec.constructor.name}`);
return "bufferedData";
}
static convert(buffer, codec) {
console.log(`Converting with ${codec.constructor.name}`);
return `converted-${buffer}`;
}
}
class AudioMixer {
fix(data) {
console.log("Fixing audio...");
return `fixed-${data}`;
}
}
// Facade: hides subsystem complexity
class VideoConverter {
convert(filename, format) {
const file = new VideoFile(filename);
const sourceCodec = new CodecFactory().extract(file);
const destinationCodec =
format === "mp4" ? new MPEG4Codec() : new OggCodec();
let buffer = BitrateReader.read(filename, sourceCodec);
let result = BitrateReader.convert(buffer, destinationCodec);
result = new AudioMixer().fix(result);
return result;
}
}
// Client: uses the Facade
const converter = new VideoConverter();
const mp4 = converter.convert("funny-cats.ogg", "mp4");
console.log("Output:", mp4);
Output:
Reading file funny-cats.ogg with OggCodec
Converting with MPEG4Codec
Fixing audio...
Output: fixed-converted-bufferedData
When to Use
- You want to simplify a complex or evolving subsystem
- You need to shield client code from low-level API changes
- You want to isolate third-party library usage
Pros & Cons
- Reduces client–subsystem coupling
- Easier to update or replace subsystem
- Simplifies usage
- Can turn into a God Object if overloaded
- Might limit access to full functionality