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. When to use Facade Design Pattern?
  3. Example to understand the Façade Design Pattern
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 Façade sits on the top of a group of subsystems and allows them to communicate in a unified manner.

Example to understand the Façade Design Pattern:

Let us understand the Façade Design Pattern with an example. Please have a look at the following diagram.

Understanding 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.

Facade Design Pattern Implementation

So, here the extra is the 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. So, the diagram of Façade Design pattern looks as shown below.

Facade Design Pattern in C#

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.

Programming Example:

Whatever we discussed up to now, let’s put into a program as shown below.

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

    //Facade 
    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....");
        }
    }

    //Sub System 1
    public class Product
    {
        public void GetProductDetails()
        {
            Console.WriteLine("Fetching the Product Details...");
        }
    }

    //Sub System 2
    public class Payment
    {
        public void MakePayment()
        {
            Console.WriteLine("Payment Done Successfully...");
        }
    }

    //Sub System 3
    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.

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 some simple examples. 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 *