The Facade pattern provides a simplified interface to a complex subsystem. It defines a higher-level interface that makes the subsystem easier to use. The client of the Facade class have no reason to access the subclasses of a Facade. For example, MS Word doesn’t need to know how to provide instructions to a specific printer (Cannon, HP, etc.) it just needs to select the printer through a Fascade class, which in turn, provides the selected printer the arguments needed to perform the print job.
Using the Facade pattern can indeed make the code more modular and easier to manage. The Facade pattern provides a simplified interface to a complex subsystem, making it easier to use and understand.
Major Differences:
Facade simplifies the interface of a complex system. It does not add new functionality but makes the existing functionality easier to use.
Example: Imagine a complex library for handling multimedia files. A Facade can provide a simple interface to play, pause, and stop media without exposing the underlying complexity.
public class MediaFacade {
private AudioPlayer audioPlayer;
private VideoPlayer videoPlayer;
public MediaFacade() {
audioPlayer = new AudioPlayer();
videoPlayer = new VideoPlayer();
}
public void playAudio(String fileName) {
audioPlayer.play(fileName);
}
public void playVideo(String fileName) {
videoPlayer.play(fileName);
}
}
Summary of Differences:
- Facade simplifies the interface of a complex system.
- Adapter converts one interface to another to make incompatible interfaces work together.
- Mediator centralizes communication between objects to reduce dependencies.
Each pattern addresses different problems and is used in different scenarios to improve code maintainability and flexibility.
This pattern is defined in Design Patterns by Gamma, Helm, Johnson, and Vlissides.