Facade Design Patten in C#

The Facade Design Pattern is a structural design pattern that provides a simplified, unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use, hiding its complexity from clients. The facade pattern is especially useful when you have a complex system with many interconnected components and you want to provide a simple interface to interact with it.

Here’s a C# example of the Facade Design Pattern with a use case for a home theater system:

Create Subsystem Classes

Define multiple classes representing various components of a home theater system, such as the DVD player, screen, and sound system.

public class DVDPlayer
{
    public void TurnOn() { Console.WriteLine("DVD player is on"); }
    public void TurnOff() { Console.WriteLine("DVD player is off"); }
    public void PlayMovie(string movie) { Console.WriteLine($"Playing movie: {movie}"); }
}

public class Screen
{
    public void PullDown() { Console.WriteLine("Screen is pulled down"); }
    public void RollUp() { Console.WriteLine("Screen is rolled up"); }
}

public class SoundSystem
{
    public void TurnOn() { Console.WriteLine("Sound system is on"); }
    public void TurnOff() { Console.WriteLine("Sound system is off"); }
    public void SetVolume(int volume) { Console.WriteLine($"Volume set to {volume}"); }
}

Create the Facade Class

Define a facade class that provides a simplified interface to the complex subsystem. This class coordinates the actions of the subsystem classes.

public class HomeTheaterFacade
{
    private DVDPlayer dvdPlayer;
    private Screen screen;
    private SoundSystem soundSystem;

    public HomeTheaterFacade()
    {
        dvdPlayer = new DVDPlayer();
        screen = new Screen();
        soundSystem = new SoundSystem();
    }

    public void WatchMovie(string movie)
    {
        screen.PullDown();
        soundSystem.TurnOn();
        soundSystem.SetVolume(10);
        dvdPlayer.TurnOn();
        dvdPlayer.PlayMovie(movie);
    }

    public void EndMovie()
    {
        screen.RollUp();
        soundSystem.TurnOff();
        dvdPlayer.TurnOff();
    }
}

Use Case – Home Theater Control

Use the facade to simplify the control of the home theater system.

class Program
{
    static void Main(string[] args)
    {
        HomeTheaterFacade homeTheater = new HomeTheaterFacade();

        Console.WriteLine("Starting movie night...");
        homeTheater.WatchMovie("The Matrix");

        Console.WriteLine("\nEnding movie night...");
        homeTheater.EndMovie();
    }
}

In this example, the HomeTheaterFacade provides a high-level interface for controlling the home theater system. The client code doesn’t need to know the complexities of individual components like the DVD player, screen, and sound system. The facade simplifies the usage of the subsystem, making it more convenient and maintainable.

The Facade Design Pattern is beneficial in scenarios where you want to hide the complexities of a subsystem and provide a user-friendly interface to interact with it.