Monday, January 8, 2024

Facade Design Pattern in C#

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.It helps encapsulate the complexity of multiple subsystems into a single unified interface.

In software terms, Facade pattern hides the complexities of the systems and provides a simple interface to the clients.

This pattern involves one wrapper class which contains a set of methods available for the client. This pattern is particularly used when a system is very complex or difficult to understand and when the system has multiple subsystems.

Component of Facade Design Pattern

  • Complex System: A library of subsystems.
  • Subsystems: These are classes within a complex system and offer detailed operations.
  • Façade: This is a wrapper class which wrapper class which contains a set of members which are required by the client.
  • Client: This is a class which calls the high-level operations in the Façade.

Example in C#

Here's an example demonstrating the Facade Pattern in C#.Let's consider a multimedia system with several complex subsystems like the AudioPlayer, VideoPlayer, and DisplaySystem.

  1. Subsystems:
    namespace FacadePattern.Subsystem
    {
        /// 
        /// Subsystem 1: AudioPlayer
        /// 
        public class AudioPlayer
        {
            public void PlayAudio(string audioFile)
            {
                Console.WriteLine("Playing audio: " + audioFile);
            }
        }
    }
        
    namespace FacadePattern.Subsystem
    {
        /// 
        /// Subsystem 2: VideoPlayer
        /// 
        public class VideoPlayer
        {
            public void PlayVideo(string videoFile)
            {
                Console.WriteLine("Playing video: " + videoFile);
            }
        }
    }
        
    namespace FacadePattern.Subsystem
    {
        /// 
        /// Subsystem 3: DisplaySystem
        /// 
        public class DisplaySystem
        {
            public void Display(string displayContent)
            {
                Console.WriteLine("Displaying: " + displayContent);
            }
        }
    }
        
  2. Facade Class(MultimediaFacade.cs):
    using FacadePattern.Subsystem;
    
    namespace FacadePattern.Facade
    {
        /// 
        /// Facade: MultimediaFacade
        /// 
        public class MultimediaFacade
        {
            private AudioPlayer audioPlayer;
            private VideoPlayer videoPlayer;
            private DisplaySystem displaySystem;
    
            public MultimediaFacade()
            {
                audioPlayer = new AudioPlayer();
                videoPlayer = new VideoPlayer();
                displaySystem = new DisplaySystem();
            }
    
            // Facade methods that provide a simpler interface
            public void PlayMusic(string audioFile)
            {
                audioPlayer.PlayAudio(audioFile);
            }
    
            public void PlayMovie(string videoFile)
            {
                videoPlayer.PlayVideo(videoFile);
            }
    
            public void DisplayContent(string content)
            {
                displaySystem.Display(content);
            }
        }
    }    
  3. Client:
    using FacadePattern.Facade;
    
    MultimediaFacade multimedia = new MultimediaFacade();
    
    // Using the facade to simplify interaction
    multimedia.PlayMusic("song.mp3");
    multimedia.PlayMovie("movie.mp4");
    multimedia.DisplayContent("Welcome to our multimedia system!");
    
    Console.ReadKey();
        

In this example, the MultimediaFacade acts as a simplified interface to the complex subsystems (AudioPlayer, VideoPlayer, DisplaySystem). The client code interacts only with the facade, which in turn delegates the tasks to the appropriate subsystems.

Output

When to use this pattern

Use this pattern to simplify the problem when there are multiple complex subsystems and interacting with them individually is really difficult/cumbersome.

Happy coding!! 😊

No comments:

Post a Comment

^ Scroll to Top