Facade Pattern – Simplify Complex Systems
🔥 The Problem Without Facade
Imagine you’re working on a video conversion app that needs to:
Each step is handled by different classes with their own methods.
If the client code needs to manage all these calls directly, it quickly becomes:
Example:
VideoFile file = new VideoFile("movie.avi");
Codec sourceCodec = new MPEG4CompressionCodec();
Codec destinationCodec = new OggCompressionCodec();
VideoFile buffer = BitrateReader.read(file, sourceCodec);
VideoFile intermediateResult = BitrateReader.convert(buffer, destinationCodec);
AudioMixer mixer = new AudioMixer();
mixer.fix(intermediateResult);
System.out.println("Conversion complete");
If you repeat this logic everywhere, your code is cluttered and fragile.
✅ What Facade Pattern Does
Facade Pattern provides a simple interface to a complex subsystem.
It wraps the complex set of operations into a single Facade class with a clear method(s).
Clients just call the Facade method(s), without worrying about the internal complexity.
💡 Real-World Analogy
Think of a universal remote control:
Recommended by LinkedIn
🧑💻 Facade Pattern Components
🧑💻 Java Implementation
1. Subsystem Classes
class VideoFile {
private String filename;
public VideoFile(String filename) {
this.filename = filename;
}
public String getFilename() {
return filename;
}
}
class Codec { /* Base codec class */ }
class MPEG4CompressionCodec extends Codec { /* Specific codec */ }
class OggCompressionCodec extends Codec { /* Specific codec */ }
class BitrateReader {
public static VideoFile read(VideoFile file, Codec codec) {
System.out.println("Reading file using codec");
// Imagine reading and decoding logic here
return file;
}
public static VideoFile convert(VideoFile buffer, Codec codec) {
System.out.println("Converting file using codec");
// Imagine converting logic here
return buffer;
}
}
class AudioMixer {
public void fix(VideoFile result) {
System.out.println("Fixing audio of the video");
// Audio fixing logic here
}
}
2. Facade Class
class VideoConverterFacade {
public void convertVideo(String filename, String format) {
VideoFile file = new VideoFile(filename);
Codec sourceCodec = extractCodec(file);
Codec destinationCodec = getCodec(format);
VideoFile buffer = BitrateReader.read(file, sourceCodec);
VideoFile intermediateResult = BitrateReader.convert(buffer, destinationCodec);
AudioMixer audioMixer = new AudioMixer();
audioMixer.fix(intermediateResult);
System.out.println("Conversion complete to format: " + format);
}
private Codec extractCodec(VideoFile file) {
System.out.println("Extracting codec from file");
// Stub: In real code, determine codec from file metadata
return new MPEG4CompressionCodec();
}
private Codec getCodec(String format) {
System.out.println("Getting codec for format: " + format);
// Stub: Return codec based on format
if ("mp4".equalsIgnoreCase(format)) return new MPEG4CompressionCodec();
else return new OggCompressionCodec();
}
}
3. Client Code
public class Client {
public static void main(String[] args) {
VideoConverterFacade converter = new VideoConverterFacade();
converter.convertVideo("movie.avi", "mp4");
}
}
💡 Why Facade Pattern Is Useful
Without Facade:
With Facade:
📌 When to Use Facade