Facade Design Pattern in C#

Facade Design Pattern in C# with Examples

In this article, I am going to discuss the Facade Design Pattern in C# with some examples. Please read our previous article where we discussed the Adapter Design Pattern in C# with examples. The Facade Design Pattern falls under the category of Structural Design Pattern. As part of this article, we are going to discuss the following pointers.

  1. What is the Facade Design Pattern in C#?
  2. Understanding the Facade Design Pattern with one real-time example.
  3. Understanding the Class Diagram of the Facade Design Pattern.
  4. When to use Facade Design Pattern?
  5. Implementing the Façade Design Pattern in C#.
What is the Facade Design Pattern in C#?

As per the GOF definition, the Façade Design Pattern states that you need to provide a unified interface to a set of interfaces in a subsystem. The Façade Design Pattern defines a higher-level interface that makes the subsystem easier to use.

In simple words, we can say that the Facade Design Pattern is used to hide the complexities of a system and provides an interface to the client using which the client can access the system. The Façade (usually a wrapper) sits on the top of a group of subsystems and allows them to communicate in a unified manner.

Understanding the Façade Design Pattern with one real-time example:

Let us understand the Façade Design Pattern with one real-time example. Please have a look at the following diagram. Here, we need to design an application to place an order.

Facade Design Pattern in C#

As shown in the above image, in order to place an order first we need to create an object of Product class and get the product details. Then if everything is fine then we need to make the Payment and in order to do this, we need to create an instance of Payment class and call the MakePayment method. If Payment is successful then we need to send the Invoice to the customer. So, in order to place the order, we need to do the above mention steps.

The Façade is actually an extra class that lies at the top of the above method class. Please have a look at the following diagram.

Understanding the Façade Design Pattern with one real-time example

So, here the extra class Order is the Façade class which will take the responsibility of placing the order. This class internally creates the instance of the respective classes and calls the method.

Understanding the class Diagram of the Facade Design Pattern:

Let us understand the class diagram and the different components involved in the Facade Design Pattern. In order to understand the Facade Design Pattern class diagram, please have a look at the following image.

Understanding the class Diagram of the Facade Design Pattern

As shown in the above image, there are two classes involved in the Facade Design Pattern. They are as follows:

The Façade class knows which subsystem classes are responsible for a given request and then it delegates the client requests to appropriate subsystem objects.

The Subsystem classes Implement their respective functionalities assigned to them and these subsystems do not have any knowledge of the facade.

Note: The façade pattern is used unknowingly so many times in our projects even we are not aware of this. This is one of the most useful design patterns. If you understand the Façade Design pattern then you will make the project architecture better.

Implementing Facade Design Pattern in C#:

Let us implement the example that we discussed step by step using the Facade Design Pattern in C#.

Step1: Creating subsystems

In our example, the systems are going to be the Product, Payment and Invoice classes and each class will have their own responsibility. So, let’s create the above three classes and implement their responsibility.

Product: Create a class file with the name Product.cs and then copy and paste the following code in it. This class is having a method to get the product details.

using System;
namespace FacadeDesignPattern
{
    public class Product
    {
        public void GetProductDetails()
        {
            Console.WriteLine("Fetching the Product Details");
        }
    }
}

Payment: Create a class file with the name Payment.cs and then copy and paste the following code in it. This class is having a method to do the payment.

using System;
namespace FacadeDesignPattern
{
    public class Payment
    {
        public void MakePayment()
        {
            Console.WriteLine("Payment Done Successfully");
        }
    }
}

Invoice: Create a class file with the name Invoice.cs and then copy and paste the following code in it. This class is having a method to send the invoice.

using System;
namespace FacadeDesignPattern
{
    public class Invoice
    {
        public void Sendinvoice()
        {
            Console.WriteLine("Invoice Send Successfully");
        }
    }
}

Note: Here, we have not implemented the methods in detail except that we are just printing the details of the methods. This is because our idea is to understand the façade design pattern implementation and not to focus on the real implementations of the methods.

Step2: Creating Facade

This is going to be a concrete class and this class takes the responsibility to place the order. So, create a class file with the name Order.cs and then copy and paste the following code in it. This class is having one method which will create subclasses objects and call the respective methods in order to place an order.

using System;
namespace FacadeDesignPattern
{
    public class Order
    {
        public void PlaceOrder()
        {
            Console.WriteLine("Place Order Started");
            Product product = new Product();
            product.GetProductDetails();
            Payment payment = new Payment();
            payment.MakePayment();
            Invoice invoice = new Invoice();
            invoice.Sendinvoice();
            Console.WriteLine("Order Placed Successfully");
        }
    }
}

Now we are hiding the complexity of creating the different subclasses objects and calling their respective methods with the help of Facade class. So, this class acts as a wrapper to the subclasses. Now the client will use this Facade class and simply call the PlaceOrder method to place an order. The PlaceOrder method takes all the responsibility to place an order.

Step3: Client

Please modify the Main method as shown below. Here, the client simply needs to create an object of Order class and call the PlaceOrder method which will place the order.

using System;
namespace FacadeDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Order order = new Order();
            order.PlaceOrder();
            Console.Read();
        }
    }
}

Output:

Facade Design Pattern in C# with real-time example

When to use Facade Design Pattern?

We need to use the Facade Design Pattern when

  1. We want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve.
  2. There are many dependencies between clients and the implementation classes

In the next article, I am going to discuss the Decorator Design Pattern in C# with some examples. Here, in this article, I try to explain the Facade Design Pattern in C# step by step with one real-time example. I hope you understood the need and use of Facade Design Pattern in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *