Here, we will see how to create Facade Design Pattern with example.
The Facade Design Pattern is a structural pattern that provides a simplified interface to a complex system. It hides the complexities of the system by exposing a unified, easy-to-use interface, allowing clients to interact with the subsystem without needing to understand the details.
Example - Home Theater System
A modern home theater system may include several components, such as a TV, speakers, DVD player, gaming console, and more. Instead of manually turning on each device, adjusting volume, and setting inputs, a home theater remote control acts as a facade. We will explain in detail lator part of the article with code example.
Home Theater System: A modern home theater system may include several components, such as a TV, speakers, DVD player, gaming console, and more. Instead of manually turning on each device, adjusting volume, and setting inputs, a home theater remote control acts as a facade. It provides a simplified interface (buttons for "Watch Movie" or "Play Game"), which internally activates the appropriate components and configures them correctly without requiring the user to understand the technical details.
Here's a deeper look at how the home theater example works in this context:
A typical home theater might include:
Normally, each of these components would require its own remote control or specific configuration:
Instead of dealing with all these components individually, the remote control acts as a facade by grouping all the commands into simple actions:
This means that the user doesn’t need to worry about which input or device to switch on. The remote hides the complexity of managing multiple systems, and the user just interacts with an easy-to-use interface.
The facade provides a unified and straightforward way to control a complex set of systems, which is the essence of the Facade Design Pattern.
using System;
// Subsystems
public class TV
{
public void TurnOn()
{
Console.WriteLine("TV is now ON");
}
public void TurnOff()
{
Console.WriteLine("TV is now OFF");
}
public void SetInput(string input)
{
Console.WriteLine($"TV input set to {input}");
}
}
public class SoundSystem
{
public void TurnOn()
{
Console.WriteLine("Sound System is now ON");
}
public void TurnOff()
{
Console.WriteLine("Sound System is now OFF");
}
public void SetVolume(int level)
{
Console.WriteLine($"Volume set to {level}");
}
}
public class DVDPlayer
{
public void TurnOn()
{
Console.WriteLine("DVD Player is now ON");
}
public void Play()
{
Console.WriteLine("Playing movie on DVD Player");
}
public void TurnOff()
{
Console.WriteLine("DVD Player is now OFF");
}
}
public class GamingConsole
{
public void TurnOn()
{
Console.WriteLine("Gaming Console is now ON");
}
public void Play()
{
Console.WriteLine("Playing game on Gaming Console");
}
public void TurnOff()
{
Console.WriteLine("Gaming Console is now OFF");
}
}
// Facade
public class HomeTheaterFacade
{
private TV tv;
private SoundSystem soundSystem;
private DVDPlayer dvdPlayer;
private GamingConsole gamingConsole;
public HomeTheaterFacade(TV tv, SoundSystem soundSystem, DVDPlayer dvdPlayer, GamingConsole gamingConsole)
{
this.tv = tv;
this.soundSystem = soundSystem;
this.dvdPlayer = dvdPlayer;
this.gamingConsole = gamingConsole;
}
public void WatchMovie()
{
Console.WriteLine("\nStarting Movie Mode...");
tv.TurnOn();
tv.SetInput("DVD Player");
soundSystem.TurnOn();
soundSystem.SetVolume(30);
dvdPlayer.TurnOn();
dvdPlayer.Play();
}
public void PlayGame()
{
Console.WriteLine("\nStarting Game Mode...");
tv.TurnOn();
tv.SetInput("Gaming Console");
soundSystem.TurnOn();
soundSystem.SetVolume(40);
gamingConsole.TurnOn();
gamingConsole.Play();
}
public void EndMovie()
{
Console.WriteLine("\nEnding Movie Mode...");
dvdPlayer.TurnOff();
soundSystem.TurnOff();
tv.TurnOff();
}
public void EndGame()
{
Console.WriteLine("\nEnding Game Mode...");
gamingConsole.TurnOff();
soundSystem.TurnOff();
tv.TurnOff();
}
}
// Client
class Program
{
static void Main(string[] args)
{
// Subsystems
TV tv = new TV();
SoundSystem soundSystem = new SoundSystem();
DVDPlayer dvdPlayer = new DVDPlayer();
GamingConsole gamingConsole = new GamingConsole();
// Facade
HomeTheaterFacade homeTheater = new HomeTheaterFacade(tv, soundSystem, dvdPlayer, gamingConsole);
// Client interaction through the Facade
homeTheater.WatchMovie();
homeTheater.EndMovie();
homeTheater.PlayGame();
homeTheater.EndGame();
}
}
Output