Back to: Design Patterns in C# With Real-Time Examples
Facade Design Pattern in C# with Examples
In this article, I am going to discuss the Facade Design Pattern in C# with Examples. Please read our previous article where we discussed Adapter Design Pattern in C# with Examples. The Facade Design Pattern falls under the category of Structural Design Pattern i.e. it is going to deal with Class and Object Composition. As part of this article, we are going to discuss the following pointers.
- What is the Facade Design Pattern in C#?
- Understanding the Facade Design Pattern with one Real-Time Example.
- Understanding the Class Diagram of Facade Design Pattern.
- When to use Facade Design Pattern?
- Implementing Facade Design Pattern in C#.
What is the Facade Design Pattern in C#?
As per the GOF definition, Facade Design Pattern states that you need to provide a unified interface to a set of interfaces in a subsystem. The Facade 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 easy-to-use interface to the client using which the client can access the system. The Facade (usually a wrapper) class sits on the top of a group of subsystems and allows them to communicate in a unified manner.
We need to use the Facade Design Pattern in C# when a system is very complex or very difficult to understand. This is because the system has a large number of interdependent classes.
As the name suggests, Facade means the Face of the Building. Suppose, you created one building. The people walking outside the building can only see the walls and glass of the Building. The People do not know anything about the wiring, the pipes, the interiors, and other complexities which are there inside the building. That means the Facade hides all the complexities of the building and displays a friendly face to people walking outside the building.
Understanding Facade Design Pattern in C# with one Real-Time Example:
Let us understand the Facade Design Pattern in C# with one Real-Time Example. Please have a look at the following diagram for a better understanding. Here, we need to design an application to place an order in an E-Commerce Application.
As shown in the above image, in order to place an order first, the Client needs to create an object of the Product class and get the product details by calling the GetProductDetails method. Then if everything is fine (i.e. if the Product is available in stock), then need to make the Payment, and in order to do this, the Client needs to create an instance of the Payment class and need to call the MakePayment method. If Payment is successful then we need to send the Invoice to the customer and to do so, the Client needs to create an instance of the Invoice class and call the SendInvoice method. So, in order to place the order, the Client needs to do the above mention steps in a particular order.
The Facade Design Pattern in C# is actually an extra class (i.e. a Wrapper class or you can say Facade Class) that sits at the top of the above classes. Please have a look at the following diagram for a better understanding.
So, here the extra class Order is nothing but the Facade class which will take the responsibility of placing the order. This class internally creates the instance of the respective classes and calls the methods in a particular order. Now, the Client will not call the respective classes and their methods to place the order, instead, the client will call the Order Class, PlaceOrder to method to place an order. The PlaceOrder method will internally use the Product, Payment, and Invoice classes to place the order.
Note: The point that you need to remember is the Facade Design Pattern not only decreases the overall complexity of the application but also it helps to move the unwanted dependencies to one place. Facade deals with interfaces, not implementation. The actual implementation is going to be provided by the Subsystems.
Understanding the Class or UML Diagram of Facade Design Pattern:
Let us understand the class diagram (or UML Diagram) and understand 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.
As shown in the above image, there are three classes involved in the Facade Design Pattern. They are as follows:
- The Facade 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 Subsystem Classes do not have any knowledge of the Facade class.
- The Client Class uses the Façade Class to access the subsystems.
Note: We have used the Facade Design Pattern unknowingly so many times in our projects even though we are not aware of this. If you are working on Web APIs, means you are working with Facade Design Patterns. This is one of the most useful Design Patterns in Real-Time Applications. If you understand the Facade Design Pattern, then you will make your 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#.
Step 1: Creating Subsystems
In our example, the Subsystem classes are going to be the Product, Payment, and Invoice classes and each class will have its own responsibility. So, let’s create the above three classes and implement their responsibility.
Product Subsystem:
Create a class file with the name Product.cs and then copy and paste the following code into it. This class is having a method to get the product details. In a real-time application, you need to call this method to get the Product details like the Product Name, Product Images, Actual Price, Discounted Price, Quantity Available, etc. The following Product class can accept the request either from the Facade or from the client directly.
using System; namespace FacadeDesignPattern { // Subsystem 1 // The Subsystem can accept requests either from the facade or from the client directly. // In this case, from the Subsystem, the Facade is also a client // Facade is not a part of the Subsystem. public class Product { public void GetProductDetails() { Console.WriteLine("Fetching the Product Details"); } } }
Payment Subsystem:
Once you make sure the Product is available, then you need to do the Payment and for this, we are going to use the Payment Subsystem. So, create a class file with the name Payment.cs and then copy and paste the following code into it. This class is having a method to do the payment. Here, you need to check how the customer is going to do the Payment. Whether Net Banking, UPI, or Cash on Delivery and accordingly you need to make the payment. The following Payment class can accept the request either from the Facade class or from the client directly.
using System; namespace FacadeDesignPattern { // Subsystem 2 // The Subsystem can accept requests either from the facade or from the client directly. // In this case, from the Subsystem, the Facade is also a client // Facade is not a part of the Subsystem. public class Payment { public void MakePayment() { Console.WriteLine("Payment Done Successfully"); } } }
Invoice Subsystem:
Once the Payment is Successful, then we need to send the Payment Invoice to the customer, and for this, we are going to use the Invoice System. So, create a class file with the name Invoice.cs and then copy and paste the following code into it. This class is having a method called Sendinvoice to send the invoice. Even sometimes, we are also sending the Payment Failed Invoice to the client. The following Invoice class can accept the request either from the Facade class or from the client directly.
using System; namespace FacadeDesignPattern { // Subsystem 3 // The Subsystem can accept requests either from the facade or from the client directly. // In this case, from the Subsystem, the Facade is also a client // Facade is not a part of the Subsystem. 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 Facade Design Pattern implementation in C# and not to focus on the real implementations of the methods.
Step 2: Creating the Facade Class
This is going to be a concrete class and this class takes the responsibility to place the order. It will work like a wrapper class. So, create a class file with the name Order.cs and then copy and paste the following code into it. This class is having one method which will create subclasses objects and call the respective methods in a particular order to place an order. The following class provides a simple interface for the client to place an order. Now, the client will create an instance of the following Order class and needs to call the PlaceOrder method to place an order.
using System; namespace FacadeDesignPattern { // The Facade class provides a simple interface to the complex logic of one // or several subsystems. The Facade delegates the client requests to the // appropriate objects within the subsystem. public class Order { public void PlaceOrder() { Console.WriteLine("Place Order Started"); //Get the Product Details Product product = new Product(); product.GetProductDetails(); //Make the Payment Payment payment = new Payment(); payment.MakePayment(); //Send the Invoice 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 the Facade class. So, this class acts as a wrapper for 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.
Step 3: Client
The class which is going to use the Facade class is nothing but the client. In our example, it is going to be the Main method of the Program class. So, please modify the Main method of the Program class as follows. Here, the client simply needs to create an object of the Order class and needs to call the PlaceOrder method to place an order.
using System; namespace FacadeDesignPattern { class Program { static void Main(string[] args) { //The Client will use the Facade Interface instead of the Subsystems Order order = new Order(); order.PlaceOrder(); Console.Read(); } } }
Output:
When to use Facade Design Patterns in Real-Time Applications?
We need to use the Facade Design Pattern in C# Real-Time Applications, when
- We want to provide a simple interface to a complex subsystem.
- There are many dependencies between clients and the implementation classes.
In the next article, I am going to discuss Real-Time Examples of Facade Design Patterns using C#. Here, in this article, I try to explain the Facade Design Pattern in C# with Examples. I hope you understood the need and use of Facade Design Patterns in C# with Examples.