The facade pattern is a Gang of Four design pattern and comes under a structural pattern as this pattern adds an interface to the existing system to hide its complexities. This pattern involves a single class which provides simplified methods required by the client and delegates calls to methods of existing system classes.

The Facade design pattern is particularly used when a system is very complex or difficult to understand because it has a large number of interdependent classes, or its source code is unavailable. The facade class is a “wrapper” that contains a set of members that are easily understood and simple to use. These members access the subsystem on behalf of the facade user, hiding the implementation details.

The UML class diagram for the implementation of the facade design pattern is given below:

Net-1

The classes, interfaces and objects in the above UML class diagram are as follows.

  1. Complex System
    A library of subsystems.
  2. SubsystemA, SubsystemB, SubsystemC
    These are classes within complex system and offer detailed operations.
  3. Facade
    This is a wrapper class which contains a set of members that are required by the client.
  4. Client
    This is a class which calls the high-level operations in the Facade.
/// <summary>
/// The 'Subsystem CarModel' class
/// </summary>
class CarModel
{
    public void SetModel()
    {
        Console.WriteLine(" CarModel - SetModel");
    }
}
 
/// <summary>
/// The 'Subsystem CarEngine' class
/// </summary>
class CarEngine
{
    public void SetEngine()
    {
        Console.WriteLine(" CarEngine - SetEngine");
    }
}
 
/// <summary>
/// The 'Subsystem CarBody' class
/// </summary>
class CarBody
{
    public void SetBody()
    {
        Console.WriteLine(" CarBody - SetBody");
    }
}
 
/// <summary>
/// The 'Subsystem CarAccessories' class
/// </summary>
class CarAccessories
{
    public void SetAccessories()
    {
        Console.WriteLine(" CarAccessories - SetAccessories");
    }
}
 
/// <summary>
/// The 'Facade' class
/// </summary>
public class CarFacade
{
    CarModel model;
    CarEngine engine;
    CarBody body;
    CarAccessories accessories;
 
    public CarFacade()
    {
        model = new CarModel();
        engine = new CarEngine();
        body = new CarBody();
        accessories = new CarAccessories();
    }
 
    public void CreateCompleteCar()
    {
        Console.WriteLine("******** Creating a Car **********n");
        model.SetModel();
        engine.SetEngine();
        body.SetBody();
        accessories.SetAccessories(); 
        Console.WriteLine("n******** Car creation complete **********");
    }
}
 
/// <summary>
/// Facade Pattern Demo
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        CarFacade facade = new CarFacade(); 
        facade.CreateCompleteCar(); 
        Console.ReadKey(); 
    }
}

 

Facade Pattern Demo – Output

Net-2