Quick Start On Facade Design Pattern

Background

One of the frequently used Structural patterns is the Facade Design pattern. This pattern simplifies the higher-level interface that makes the subsystem easier to use and hides the complexity of the system.

Real-world Analogy

Let’s put technical terminology aside and understand the common terms.

If you look around the real world, there are a lot of examples that will help you understand Facade Pattern.

  1. E-commerce website to place an order with a selected product, payment, and invoice generation.
  2. Home Automation to turn on/off the electronic appliances when unlocking/locking of main house door and control the devices like AC, Television, Geyser, Fan, Tube lights, music system, camera and many more.
  3. A mobile shopkeeper having different brands of mobile phones to sell like Nokia, Samsung, iPhone, Motorola, and many more.
  4. An encryption library to generate the hash of data with a different algorithm like SHA, SHA256, SHA512, MD5, and many more
  5. And many more. If you know something unique, please do post it in a comment box.

Example with Illustration

Let’s consider a laptop. When we want to turn ON the Laptop, we just press a single button to turn ON the computer, and internally many subsystems are inter-connected like Motherboard, Hard Drive, RAM, Graphics card, Wi-Fi device, Bluetooth, Keyboard, Mouse pad, and more. All those subsystems combine and forms as a single unit. Here once the system turns ON, Motherboard, RAM, Graphics card, Hard drive, Keyboard, Mouse pad will start functioning, whereas Bluetooth and Wi-Fi device can function based on individual settings to turn on/off.

 

When can we use the Facade design pattern?

  1. When the system has many complex subsystem/modules and needs to use a combination as a single system.
  2. When you want to isolate the code from the complex subsystem.

Pros

  1. Isolates your code from the complexity of a subsystem.
  2. Very easy to access the group of subsystems in the Facade module.

Cons

  1. It acts as the main object that is coupled to all classes.

UML Representation

Code Example

Home Automation to control the appliances.

Let’s consider a Home Automation to turn ON/OFF the electronic appliances when unlocking/locking the main door of the house and control the devices like AC, Television, Geyser, Fan, Tube lights, Music system, Camera and many more.

Let's refer to the above UML diagram to represent Home Automation. Here we can consider Subsystem as ‘AirConditioner’, ‘HomeSecurity’, ‘Lights’, and ‘MusicSystem’. All the subsystems are independent in functionality.

Define 'Subsystem', here Subsystem as the "AirConditioner" class has the functionality to turn ON/OFF the device.

namespace DP.FacadeDesignPattern.Subsystem  
{  
    using System;  
  
    public class AirConditioner  
    {  
        public void TurnOnAirConditioner()  
        {  
            Console.WriteLine("TURN ON AC");  
        }  
  
        public void TurnOffAirConditioner()  
        {  
            Console.WriteLine("TURN OFF AC");  
        }  
    }  
}  

Define 'Subsystem', here Subsystem as the "HomeSecurity" class has the functionality to turn ON/OFF the device.

namespace DP.FacadeDesignPattern.Subsystem  
{  
    using System;  
  
    public class HomeSecurity  
    {  
        public void TurnOnHomeSecurity()  
        {  
            Console.WriteLine("TURN ON HOME SECURITY");  
        }  
  
        public void TurnOffHomeSecurity()  
        {  
            Console.WriteLine("TURN OFF HOME SECURITY");  
        }  
    }  
}  

Define 'Subsystem', here Subsystem as "Lights" class has the functionality to turn ON/OFF the device.

namespace DP.FacadeDesignPattern.Subsystem  
{  
    using System;  
  
    public class Lights  
    {  
        public void TurnOnLights()  
        {  
            Console.WriteLine("TURN ON LIGHTS");  
        }  
  
        public void TurnOffLights()  
        {  
            Console.WriteLine("TURN OFF LIGHTS");  
        }  
    }  
}  

Define 'Subsystem', here Subsystem as "MusicSystem" class has the functionality to turn ON/OFF the device.

namespace DP.FacadeDesignPattern.Subsystem  
{  
    using System;  
  
    public class MusicSystem  
    {  
        public void TurnOnMusicSystem()  
        {  
            Console.WriteLine("TURN ON MUSIC");  
        }  
  
        public void TurnOffMusicSystem()  
        {  
            Console.WriteLine("TURN OFF MUSIC");  
        }  
    }  
}  

Define 'Facade', here Facade as ‘HomeFacade’ class to control the appliances, which allows two actions to control the set of appliances.

  • When a person unlocks the main door of the house and gets in the house,  it should turn ON the Lights, AC, and Music system, and should turn OFF the Security system by calling a function "ArriveAtHome()".
  • When a person locks the main door of the house and moves out from the house, should turn OFF the Lights, AC, and Music system, and should turn ON the Security system by calling a function "LeaveFromHome()".

Here 'HomeFacade'allows easy access to any subsystems and isolates the code from the complexity of the subsystem to consume by the Facade. 

namespace DP.FacadeDesignPattern.Facade  
{  
    using DP.FacadeDesignPattern.Subsystem;  
  
    public class HomeFacade  
    {  
        private Lights _lights = null;  
        private AirConditioner _airConditioner = null;  
        private MusicSystem _musicSystem = null;  
        private HomeSecurity _homeSecurity = null;  
  
        public HomeFacade()  
        {  
            _lights = new Lights();  
            _airConditioner = new AirConditioner();  
            _musicSystem = new MusicSystem();  
            _homeSecurity = new HomeSecurity();  
        }  
  
        public void ArriveAtHome()  
        {  
            _lights.TurnOnLights();  
            _musicSystem.TurnOnMusicSystem();  
            _airConditioner.TurnOnAirConditioner();  
            _homeSecurity.TurnOffHomeSecurity();  
        }  
  
        public void LeaveFromHome()  
        {  
            _lights.TurnOffLights();  
            _musicSystem.TurnOffMusicSystem();  
            _airConditioner.TurnOffAirConditioner();  
            _homeSecurity.TurnOnHomeSecurity();  
        }  
    }  
}  

Client implementation will be very simple and straightforward to consume the ‘HomeFacade’ class and calls the required Facade functionality to provide easy access to a set of subsystems and hides from the complexity of the system.

namespace DP.FacadeDesignPattern  
{  
    using System;  
    using DP.FacadeDesignPattern.Facade;  
  
    /// <summary>  
    /// The higher-level interface that makes the subsystem easier to use  
    /// </summary>  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            HomeFacade homeFacade = new HomeFacade();  
            homeFacade.LeaveFromHome();  
            Console.ReadKey();  
        }  
    }  
}  

Output

Here we are able to demonstrate the Home automation with “LeaveFromHome” functionality and controls the set of home appliances. In the output, we could see that, turned OFF the Lights, AC, and Music system; and turned ON the Security system. The working of the system is very simple by implementing with Facade design pattern. I have attached the code sample, that would help you to play around with the implementation.

I hope this article helps you to understand this simple approach. Thanks for reading the article.