knowledge/technology/dev/programming/patterns/structural/Facade Pattern.md
2024-01-17 09:00:45 +01:00

66 lines
No EOL
3.7 KiB
Markdown

---
obj: concept
---
# Facade Pattern
## **Description:**
The Facade Pattern is a structural design pattern that provides a simplified and unified interface to a set of interfaces in a subsystem. It acts as a front-facing interface to a complex system, making it easier for clients to interact with that system. The Facade Pattern promotes loose coupling and encapsulation by hiding the complexities of the subsystem behind a simple interface.
**How it's Used:**
1. **Subsystem**: Identify a complex subsystem or set of related classes that perform various tasks but have a complex and possibly fragmented interface.
2. **Facade Class**: Create a facade class that encapsulates the subsystem's functionality and provides a simplified, high-level interface for clients.
3. **Facade Methods**: Define methods in the facade class that correspond to the common operations or workflows that clients need to perform within the subsystem.
4. **Client Code**: Clients interact with the subsystem through the facade class by calling its methods. Clients are shielded from the complexities of the subsystem.
## **Example:**
Imagine you are developing a multimedia processing library with various components for image processing, audio processing, and video processing. Each component has its own set of classes and interfaces, and interacting with them individually can be complex. You can use the Facade Pattern to create a simplified interface for clients:
- **Subsystem Classes**: Define classes for image processing, audio processing, and video processing, each with its own set of methods and complexities.
- **Facade Class**: Create a `MultimediaProcessor` facade class that provides high-level methods for common multimedia operations, such as `processImage`, `processAudio`, and `processVideo`.
```java
public class MultimediaProcessor {
private ImageProcessor imageProcessor;
private AudioProcessor audioProcessor;
private VideoProcessor videoProcessor;
public MultimediaProcessor() {
this.imageProcessor = new ImageProcessor();
this.audioProcessor = new AudioProcessor();
this.videoProcessor = new VideoProcessor();
}
public void processImage(String imagePath) {
imageProcessor.loadImage(imagePath);
imageProcessor.applyFilters();
imageProcessor.saveImage();
}
public void processAudio(String audioPath) {
audioProcessor.loadAudio(audioPath);
audioProcessor.adjustVolume();
audioProcessor.saveAudio();
}
public void processVideo(String videoPath) {
videoProcessor.loadVideo(videoPath);
videoProcessor.extractAudio();
videoProcessor.convertToFormat();
videoProcessor.saveVideo();
}
}
```
- **Client Code**: In your client code, use the `MultimediaProcessor` facade class to perform multimedia operations without needing to interact with the individual image, audio, and video processing components directly.
```java
public class Client {
public static void main(String[] args) {
MultimediaProcessor multimediaProcessor = new MultimediaProcessor();
multimediaProcessor.processImage("sample.jpg");
multimediaProcessor.processAudio("sample.mp3");
multimediaProcessor.processVideo("sample.mp4");
}
}
```
By using the Facade Pattern, you provide a simplified and unified interface (`MultimediaProcessor`) for clients to perform multimedia processing operations. Clients don't need to be aware of the individual complexities and intricacies of the subsystem components (image processing, audio processing, video processing), promoting code modularity and ease of use. The Facade Pattern is particularly valuable when dealing with complex systems or libraries with numerous components.