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.

  • MultimediaFacade class that acts as a single entry point for interacting with the multimedia subsystem.
  • AudioPlayer, VideoPlayer, and ImageLoader, providing a simple playMedia method that takes a filename and media type as parameters.
  • Client code can then use the facade to play different types of media without worrying about the details of each subsystem component.

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.

AudioPlayer




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


VideoPlayer




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


ImageLoader




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.

Java




// 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:

Java




// 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:

Java




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








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

Important Topics for the Facade Method Design Pattern in Java

  • What is the Facade Method Design Pattern in Java?
  • Why do we need Facade Method Design Pattern in Java
  • Key Component of Facade Method Design Pattern in Java
  • Implementation of Facade Method Design Pattern in Java
  • Use Cases of Facade Method Design Pattern in Java
  • Advantages of Facade Method Design Pattern in Java
  • Disadvantages of Facade Method Design Pattern in Java

Similar Reads

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....

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

Example:...

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:...

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

...