Blog / Design Patterns
The Facade Design Pattern in C# and .Net
  • Dec 25, 2020
  • 92
  • 82

The fifth design pattern in the Structural Category is the Facade Design Pattern. Let's take a look on what is this design pattern for and how do we implement it in C# and .Net.

Note: Code can be downloaded at my Github.


Advertisement
 


1. What is the Facade Design Pattern?

The Facade Design Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a high level interface that makes the subsystem easier to use.

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

2. When to implement the Facade Design Pattern?

The Facade Design Pattern may be used unknowingly in your project even you are not aware of this. This is one of the most useful design pattern which helps your project architecture better if you understand it thoroughly.

You may want to implement the Facade Design Pattern when:

  • You want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve.
  • There are many dependencies between clients and the implementation classes of an abstraction.
  • You want to layer the subsystems. Use a facade to define an entry point to each subsystem level.

3. How to implement the Facade Design Pattern?

Let's take an example of an application involving payment which has a Product, a Payment, and an Invoice class.

Step 1: Examine the Subsystems

The existing system has 3 subsystems as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Payment
{
    public void MakePayment()
    {
        Console.WriteLine("Payment Proceeded Successfully!");
    }
}
 
public class Invoice
{
    public void SendInvoice()
    {
        Console.WriteLine("Invoice Sent Successfully!");
    }
}

Step 2: Create a Facade class

Creating an OrderFacade class to make a gateway to all subsystems:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class OrderFacade
{
    public void PlaceOrder()
    {
        Console.WriteLine("---Ordering Proccess Started---");
        Product p = new Product();
        p.GetProductDetails();
        Payment pmt = new Payment();
        pmt.MakePayment();
        Invoice inv = new Invoice();
        inv.SendInvoice();
        Console.WriteLine("Order Placed Successfully!");
        Console.WriteLine("---Ordering Proccess Ended---");
    }
}

Done! So now in your client, you don't need to directly access the subsystems, instead, you can just use the Facade to let it process your subsystems internally.

1
2
3
4
5
6
7
8
class Program
{
    static void Main(string[] args)
    {
        OrderFacade o = new OrderFacade();
        o.PlaceOrder();
    }
}

Output


4. Conclusion

The Facade Design Pattern is a way to simplify your complex systems to make it easier for the client to use. I hope this tutorial is helpful and please let me know your thoughts by commenting this the section below. See you next time!


There are some benefits of keeping both UI and API parts in the same place for small projects. In this article, I will explain how I did to deploy Angular Web and ASP .Net Core API in the same folder ...

Using cherry-pick to select specific commits for your Pull Request.1. Create a new branch based on the target of the Pull Requestgit branch cherry-branch origin/master2. Switch to a new branchgit chec ...

I got CORS error after publishing my API and Angular app to IIS even though CORS is enabled and the origins of the Angular app is added. Below is how I resolved this issue.Just simple, make sure you s ...

After deployment Angular and API on IIS, it's working fine unless I refresh the page. Once refreshed, the web encountered 404 error. In this article, I will explain how to resolve this.Since Angular i ...

1. The Situation:Error Message: Pulse Secure Application failed to load Java. Please install correct JRE version.Description: This issue happens when I'm using a M1 Mac with a correct version of ...

Accelerated Mobile Pages (AMP) focuses on delivering static content from publishers as quickly as possible and possibly rewards early adopters with a boost in rank. Let's see how to implement it ...

Below is how to decrypt/convert a Hex string value into text using VB.Net:Decrypting Hex string value to string in VB.Net Function HexToString(ByVal hex As String) As String Dim text As New Sy ...

After a month of publishing on Google Play, Jungle Words has made it to the Top Android Games To Try Out In April 2021. Please check it out! GameKeys.netGameKeys is a website which introduces gam ...

Centering HTML elements is an important aspect for anything involving designing with CSS. There are various methods of centering things, however, it also depends on the elements that you are working w ...