Open In App

Facade Method Design Pattern in Java

Facade Method Design Pattern is a structural design pattern that provides a simplified interface to a complex subsystem. It acts as a “front door,” concealing the internal complexity of the subsystem and making it easier for clients to interact with it. In this article, we will get to know about what is Facade Method Design Pattern in Java, and why we need Facade Method Design Pattern in Java, with the help of a problem statement and solution

1. What is the Facade Method Design Pattern in Java?

Facade Design Pattern is a structural design pattern that provides a simplified interface to a set of interfaces in a subsystem, making it easier to use.



The functionality of Facade Method Design Patterns in Java are :

2. Why do we need Facade Method Design Pattern in Java

Example:

A BankingFacade could provide simple methods for account creation, transactions, and balance inquiries, hiding the internal workings of different account types, transaction processing, and security measures.

3. Key Component of Facade Method Design Pattern in Java

The key components of the Facade Method Design Pattern in Java:

4. Implementation of Facade Method Design Pattern in Java

Problem Statement:

You are working on a multimedia application that handles various types of media, including audio files, video files, and image files. The application needs to provide a simple and unified interface for playing audio, video, and loading images.

Step wise Step implementation of Facade Method Design Pattern

We will see the implementation of Facade Method Design Pattern

4.1 Subsystem Class:

The subsystem Code works as an functionality any class, from the above example we will make the AudioPlayer, VideoPlayer , and ImageLoader as an class. But the user is not able to see what is happening behind the User Interface.




class AudioPlayer {
    void playAudio(String filename) {
        System.out.println("Playing audio file: " + filename);
    }
}




class VideoPlayer {
    void playVideo(String filename) {
        System.out.println("Playing video file: " + filename);
    }
}




class ImageLoader {
    void loadImage(String filename) {
        System.out.println("Loading image file: " + filename);
    }
}

4.2 Facade Class:

Here Facade class is Multimedia class. We can take the example of UserInterface here UI is Facade and the code, api server side programming all the thing comes in Sybsystem class.




// Facade class
class MultimediaFacade {
    private AudioPlayer audioPlayer;
    private VideoPlayer videoPlayer;
    private ImageLoader imageLoader;
 
    public MultimediaFacade() {
        this.audioPlayer = new AudioPlayer();
        this.videoPlayer = new VideoPlayer();
        this.imageLoader = new ImageLoader();
    }
 
    void playMedia(String filename, String mediaType) {
        if (mediaType.equals("audio")) {
            audioPlayer.playAudio(filename);
        } else if (mediaType.equals("video")) {
            videoPlayer.playVideo(filename);
        } else if (mediaType.equals("image")) {
            imageLoader.loadImage(filename);
        } else {
            System.out.println("Unsupported media type: " + mediaType);
        }
    }
}

4.3 Client Class:




// Client code using the facade
public class Client {
    public static void main(String[] args) {
        MultimediaFacade facade = new MultimediaFacade();
 
        // Playing audio
        facade.playMedia("song.mp3", "audio");
 
        // Playing video
        facade.playMedia("movie.mp4", "video");
 
        // Loading image
        facade.playMedia("picture.jpg", "image");
 
        // Unsupported media type
        facade.playMedia("unknown.file", "unknown");
    }
}

4.4 Overall Code for the above Implementation:




import java.util.*;
// Subsystem components
class AudioPlayer {
    void playAudio(String filename) {
        System.out.println("Playing audio file: " + filename);
    }
}
 
class VideoPlayer {
    void playVideo(String filename) {
        System.out.println("Playing video file: " + filename);
    }
}
 
class ImageLoader {
    void loadImage(String filename) {
        System.out.println("Loading image file: " + filename);
    }
}
 
// Facade class
class MultimediaFacade {
    private AudioPlayer audioPlayer;
    private VideoPlayer videoPlayer;
    private ImageLoader imageLoader;
 
    public MultimediaFacade() {
        this.audioPlayer = new AudioPlayer();
        this.videoPlayer = new VideoPlayer();
        this.imageLoader = new ImageLoader();
    }
 
    void playMedia(String filename, String mediaType) {
        if (mediaType.equals("audio")) {
            audioPlayer.playAudio(filename);
        } else if (mediaType.equals("video")) {
            videoPlayer.playVideo(filename);
        } else if (mediaType.equals("image")) {
            imageLoader.loadImage(filename);
        } else {
            System.out.println("Unsupported media type: " + mediaType);
        }
    }
}
 
// Client code using the facade
public class Main {
    public static void main(String[] args) {
        MultimediaFacade facade = new MultimediaFacade();
 
        // Playing audio
        facade.playMedia("song.mp3", "audio");
 
        // Playing video
        facade.playMedia("movie.mp4", "video");
 
        // Loading image
        facade.playMedia("picture.jpg", "image");
 
        // Unsupported media type
        facade.playMedia("unknown.file", "unknown");
    }
}

Output
Playing audio file: song.mp3
Playing video file: movie.mp4
Loading image file: picture.jpg
Unsupported media type: unknown








5. Use Cases of Facade Method Design Pattern in Java

6. Advantages of Facade Method Design Pattern in Java

7. Disadvantages of Facade Method Design Pattern in Java

8. Conclusion

The facade pattern is appropriate when you have a complex system that you want to expose to clients in a simplified way, or you want to make an external communication layer over an existing system that is incompatible with the system. Facade deals with interfaces, not implementation. Its purpose is to hide internal complexity behind a single interface that appears simple on the outside.


Article Tags :