Facade Design Pattern
We have many Design patterns. Currently, we will discuss Facade.
Details:
The Facade Pattern is a structural design pattern that provides a simplified interface to a complex subsystem or set of interfaces. It encapsulates the complexity of the subsystem and provides a higher-level interface that clients can use to interact with the subsystem more easily.
Let’s consider an example to understand the Facade Pattern:
Suppose you are building a multimedia playback application that can play various types of media files, such as audio files, video files, and streaming media. The multimedia subsystem consists of multiple classes and interfaces for handling different media formats, codecs, streaming protocols, and playback controls.
Without using the Facade Pattern, clients of your application would need to understand and interact with all the intricacies of the multimedia subsystem directly, which could be complex and cumbersome.
Here’s where the Facade Pattern comes in:
Get Amit Dhiman’s stories in your inbox
Join Medium for free to get updates from this writer.
Create a Facade Class:
- Create a class called
MediaPlayerFacadethat acts as a facade for the multimedia subsystem. - The
MediaPlayerFacadeclass provides a simple and unified interface that encapsulates the complexity of the underlying subsystem.
Implement Simplified Methods:
- Within the
MediaPlayerFacadeclass, implement simplified methods that handle common operations clients may perform, such asplayAudio(),playVideo(), orplayStream(). - These methods internally interact with the appropriate classes and components of the multimedia subsystem to perform the requested operations.
Hide Complexity from Clients:
- Clients of your application can now interact with the
MediaPlayerFacadeclass instead of directly accessing the subsystem's classes and interfaces. - The
MediaPlayerFacadeclass shields clients from the complexity of the subsystem by providing a higher-level, intuitive interface.
Here’s a simplified example implementation of the Facade Pattern for our multimedia playback application:
// Facade class
public class MediaPlayerFacade {
private AudioPlayer audioPlayer;
private VideoPlayer videoPlayer;
private StreamingPlayer streamingPlayer;
public MediaPlayerFacade() {
audioPlayer = new AudioPlayer();
videoPlayer = new VideoPlayer();
streamingPlayer = new StreamingPlayer();
}
public void playAudio(String audioFile) {
audioPlayer.loadAudio(audioFile);
audioPlayer.play();
}
public void playVideo(String videoFile) {
videoPlayer.loadVideo(videoFile);
videoPlayer.play();
}
public void playStream(String streamUrl) {
streamingPlayer.openStream(streamUrl);
streamingPlayer.play();
}
}In this example, the MediaPlayerFacade class provides simplified methods playAudio(), playVideo(), and playStream() that internally interact with the AudioPlayer, VideoPlayer, and StreamingPlayer classes respectively, which are part of the multimedia subsystem.
Now, clients of the application can simply create an instance of the MediaPlayerFacade class and use its methods to play audio files, video files, or streaming media without needing to understand the inner workings and complexities of the underlying subsystem.
MediaPlayerFacade mediaPlayer = new MediaPlayerFacade();
mediaPlayer.playAudio("audio.mp3");
mediaPlayer.playVideo("video.mp4");
mediaPlayer.playStream("http://example.com/stream");The Facade Pattern simplifies the usage and integration of complex subsystems, improves code maintainability, and reduces the dependency of clients on the subsystem’s implementation details.
By providing a unified interface, the Facade Pattern promotes a clear separation between the subsystem and its clients, making the codebase more manageable and easier to understand.




